Mercurial > hg > easyhg
changeset 50:c76782c14371
* Use specific includes instead of QtCore, QtGui etc; bring in TextAbbrev class
author | Chris Cannam |
---|---|
date | Thu, 11 Nov 2010 22:04:59 +0000 |
parents | f9b53c10a3f6 |
children | bf3ab0ffb559 |
files | common.h easyhg.pro hgexpwidget.cpp hgexpwidget.h hgrunner.cpp main.cpp mainwindow.cpp mainwindow.h settingsdialog.cpp textabbrev.cpp textabbrev.h |
diffstat | 11 files changed, 417 insertions(+), 20 deletions(-) [+] |
line wrap: on
line diff
--- a/common.h Wed Nov 10 22:27:58 2010 +0000 +++ b/common.h Thu Nov 11 22:04:59 2010 +0000 @@ -5,7 +5,7 @@ ** Copyright (C) Jari Korhonen, 2010 (under lgpl) ****************************************************************************/ -#include <QtCore> +#include <QString> #define APPNAME "HgExplorer" #define APPVERSION "0.7.2"
--- a/easyhg.pro Wed Nov 10 22:27:58 2010 +0000 +++ b/easyhg.pro Thu Nov 11 22:04:59 2010 +0000 @@ -18,7 +18,8 @@ logparser.h \ panner.h \ panned.h \ - connectionitem.h + connectionitem.h \ + textabbrev.h SOURCES = main.cpp \ mainwindow.cpp \ hgexpwidget.cpp \ @@ -31,7 +32,8 @@ logparser.cpp \ panner.cpp \ panned.cpp \ - connectionitem.cpp + connectionitem.cpp \ + textabbrev.cpp # ! [0] RESOURCES = hgexplorer.qrc
--- a/hgexpwidget.cpp Wed Nov 10 22:27:58 2010 +0000 +++ b/hgexpwidget.cpp Thu Nov 11 22:04:59 2010 +0000 @@ -2,10 +2,6 @@ ** Copyright (C) Jari Korhonen, 2010 (under lgpl) ****************************************************************************/ -#include <QtGui> -#include <QtCore> -#include <QClipboard> - #include "hgexpwidget.h" #include "common.h" #include "logparser.h" @@ -15,6 +11,10 @@ #include "panner.h" #include "panned.h" +#include <QClipboard> +#include <QContextMenuEvent> +#include <QApplication> + #include <iostream> #define REMOTE_REPO_STR "Remote repository: "
--- a/hgexpwidget.h Wed Nov 10 22:27:58 2010 +0000 +++ b/hgexpwidget.h Thu Nov 11 22:04:59 2010 +0000 @@ -5,13 +5,16 @@ ** Copyright (C) Jari Korhonen, 2010 (under lgpl) ****************************************************************************/ -#include <QtGui> -#include <QtCore> -#include <QMenu> - #include "changeset.h" #include "common.h" +#include <QMenu> +#include <QListWidget> +#include <QGroupBox> +#include <QVBoxLayout> +#include <QCheckBox> +#include <QLabel> + #define NUM_STAT_FILE_TYPES 7
--- a/hgrunner.cpp Wed Nov 10 22:27:58 2010 +0000 +++ b/hgrunner.cpp Thu Nov 11 22:04:59 2010 +0000 @@ -3,8 +3,12 @@ ****************************************************************************/ #include "hgrunner.h" -#include <QtCore> -#include <QtGui> + +#include <QPushButton> +#include <QListWidget> +#include <QDialog> +#include <QLabel> +#include <QVBoxLayout> #include <iostream> #include <unistd.h>
--- a/main.cpp Wed Nov 10 22:27:58 2010 +0000 +++ b/main.cpp Thu Nov 11 22:04:59 2010 +0000 @@ -21,7 +21,7 @@ QApplication app(argc, argv); app.setApplicationName(APPNAME); -/* +/*!!! #ifdef Q_WS_X11 app.setStyle(new QCleanlooksStyle); #endif
--- a/mainwindow.cpp Wed Nov 10 22:27:58 2010 +0000 +++ b/mainwindow.cpp Thu Nov 11 22:04:59 2010 +0000 @@ -2,16 +2,21 @@ ** Copyright (C) Jari Korhonen, 2010 (under lgpl) ****************************************************************************/ -#include <QtGui> +#include "mainwindow.h" +#include "settingsdialog.h" + #include <QStringList> #include <QDir> #include <QNetworkInterface> #include <QHostAddress> #include <QHostInfo> #include <QDesktopServices> - -#include "mainwindow.h" -#include "settingsdialog.h" +#include <QStatusBar> +#include <QMessageBox> +#include <QMenuBar> +#include <QApplication> +#include <QToolBar> +#include <QSettings> MainWindow::MainWindow()
--- a/mainwindow.h Wed Nov 10 22:27:58 2010 +0000 +++ b/mainwindow.h Thu Nov 11 22:04:59 2010 +0000 @@ -5,12 +5,13 @@ #ifndef MAINWINDOW_H #define MAINWINDOW_H -#include <QMainWindow> - #include "hgexpwidget.h" #include "hgrunner.h" #include "common.h" +#include <QMainWindow> +#include <QListWidget> + QT_BEGIN_NAMESPACE class QAction; class QMenu;
--- a/settingsdialog.cpp Wed Nov 10 22:27:58 2010 +0000 +++ b/settingsdialog.cpp Thu Nov 11 22:04:59 2010 +0000 @@ -6,6 +6,9 @@ #include <QHBoxLayout> #include <QVBoxLayout> +#include <QDir> +#include <QMessageBox> +#include <QFileDialog> SettingsDialog::SettingsDialog(QWidget *parent): QDialog(parent) {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/textabbrev.cpp Thu Nov 11 22:04:59 2010 +0000 @@ -0,0 +1,271 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + Sonic Visualiser + An audio file viewer and annotation editor. + Centre for Digital Music, Queen Mary, University of London. + This file copyright 2006-2007 Chris Cannam and QMUL. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. See the file + COPYING included with this distribution for more information. +*/ + +#include "textabbrev.h" + +#include <QFontMetrics> +#include <QApplication> + +#include <iostream> + +QString +TextAbbrev::getDefaultEllipsis() +{ + return "..."; +} + +int +TextAbbrev::getFuzzLength(QString ellipsis) +{ + int len = ellipsis.length(); + if (len < 3) return len + 3; + else if (len > 5) return len + 5; + else return len * 2; +} + +int +TextAbbrev::getFuzzWidth(const QFontMetrics &metrics, QString ellipsis) +{ + int width = metrics.width(ellipsis); + return width * 2; +} + +QString +TextAbbrev::abbreviateTo(QString text, int characters, Policy policy, + QString ellipsis) +{ + switch (policy) { + + case ElideEnd: + case ElideEndAndCommonPrefixes: + text = text.left(characters) + ellipsis; + break; + + case ElideStart: + text = ellipsis + text.right(characters); + break; + + case ElideMiddle: + if (characters > 2) { + text = text.left(characters/2 + 1) + ellipsis + + text.right(characters - (characters/2 + 1)); + } else { + text = text.left(characters) + ellipsis; + } + break; + } + + return text; +} + +QString +TextAbbrev::abbreviate(QString text, int maxLength, Policy policy, bool fuzzy, + QString ellipsis) +{ + if (ellipsis == "") ellipsis = getDefaultEllipsis(); + int fl = (fuzzy ? getFuzzLength(ellipsis) : 0); + if (maxLength <= ellipsis.length()) maxLength = ellipsis.length() + 1; + if (text.length() <= maxLength + fl) return text; + + int truncated = maxLength - ellipsis.length(); + return abbreviateTo(text, truncated, policy, ellipsis); +} + +QString +TextAbbrev::abbreviate(QString text, + const QFontMetrics &metrics, int &maxWidth, + Policy policy, QString ellipsis) +{ + if (ellipsis == "") ellipsis = getDefaultEllipsis(); + + int tw = metrics.width(text); + + if (tw <= maxWidth) { + maxWidth = tw; + return text; + } + + int truncated = text.length(); + QString original = text; + + while (tw > maxWidth && truncated > 1) { + + truncated--; + + if (truncated > ellipsis.length()) { + text = abbreviateTo(original, truncated, policy, ellipsis); + } else { + break; + } + + tw = metrics.width(text); + } + + maxWidth = tw; + return text; +} + +QStringList +TextAbbrev::abbreviate(const QStringList &texts, int maxLength, + Policy policy, bool fuzzy, QString ellipsis) +{ + if (policy == ElideEndAndCommonPrefixes && + texts.size() > 1) { + + if (ellipsis == "") ellipsis = getDefaultEllipsis(); + int fl = (fuzzy ? getFuzzLength(ellipsis) : 0); + if (maxLength <= ellipsis.length()) maxLength = ellipsis.length() + 1; + + int maxOrigLength = 0; + for (int i = 0; i < texts.size(); ++i) { + int len = texts[i].length(); + if (len > maxOrigLength) maxOrigLength = len; + } + if (maxOrigLength <= maxLength + fl) return texts; + + return abbreviate(elidePrefixes + (texts, maxOrigLength - maxLength, ellipsis), + maxLength, ElideEnd, fuzzy, ellipsis); + } + + QStringList results; + for (int i = 0; i < texts.size(); ++i) { + results.push_back + (abbreviate(texts[i], maxLength, policy, fuzzy, ellipsis)); + } + return results; +} + +QStringList +TextAbbrev::abbreviate(const QStringList &texts, const QFontMetrics &metrics, + int &maxWidth, Policy policy, QString ellipsis) +{ + if (policy == ElideEndAndCommonPrefixes && + texts.size() > 1) { + + if (ellipsis == "") ellipsis = getDefaultEllipsis(); + + int maxOrigWidth = 0; + for (int i = 0; i < texts.size(); ++i) { + int w = metrics.width(texts[i]); + if (w > maxOrigWidth) maxOrigWidth = w; + } + + return abbreviate(elidePrefixes(texts, metrics, + maxOrigWidth - maxWidth, ellipsis), + metrics, maxWidth, ElideEnd, ellipsis); + } + + QStringList results; + int maxAbbrWidth = 0; + for (int i = 0; i < texts.size(); ++i) { + int width = maxWidth; + QString abbr = abbreviate(texts[i], metrics, width, policy, ellipsis); + if (width > maxAbbrWidth) maxAbbrWidth = width; + results.push_back(abbr); + } + maxWidth = maxAbbrWidth; + return results; +} + +QStringList +TextAbbrev::elidePrefixes(const QStringList &texts, + int targetReduction, + QString ellipsis) +{ + if (texts.empty()) return texts; + int plen = getPrefixLength(texts); + int fl = getFuzzLength(ellipsis); + if (plen < fl) return texts; + + QString prefix = texts[0].left(plen); + int truncated = plen; + if (plen >= targetReduction + fl) { + truncated = plen - targetReduction; + } else { + truncated = fl; + } + prefix = abbreviate(prefix, truncated, ElideEnd, false, ellipsis); + + QStringList results; + for (int i = 0; i < texts.size(); ++i) { + results.push_back + (prefix + texts[i].right(texts[i].length() - plen)); + } + return results; +} + +QStringList +TextAbbrev::elidePrefixes(const QStringList &texts, + const QFontMetrics &metrics, + int targetWidthReduction, + QString ellipsis) +{ + if (texts.empty()) return texts; + int plen = getPrefixLength(texts); + int fl = getFuzzLength(ellipsis); + if (plen < fl) return texts; + + QString prefix = texts[0].left(plen); + int pwid = metrics.width(prefix); + int twid = pwid - targetWidthReduction; + if (twid < metrics.width(ellipsis) * 2) twid = metrics.width(ellipsis) * 2; + prefix = abbreviate(prefix, metrics, twid, ElideEnd, ellipsis); + + QStringList results; + for (int i = 0; i < texts.size(); ++i) { + results.push_back + (prefix + texts[i].right(texts[i].length() - plen)); + } + return results; +} + +static bool +havePrefix(QString prefix, const QStringList &texts) +{ + for (int i = 1; i < texts.size(); ++i) { + if (!texts[i].startsWith(prefix)) return false; + } + return true; +} + +int +TextAbbrev::getPrefixLength(const QStringList &texts) +{ + QString reference = texts[0]; + + if (reference == "" || havePrefix(reference, texts)) { + return reference.length(); + } + + int candidate = reference.length(); + QString splitChars(";:,./#-!()$_+=[]{}\\"); + + while (--candidate > 1) { + if (splitChars.contains(reference[candidate])) { + if (havePrefix(reference.left(candidate), texts)) { + break; + } + } + } + +// std::cerr << "TextAbbrev::getPrefixLength: prefix length is " << candidate << std::endl; +// for (int i = 0; i < texts.size(); ++i) { +// std::cerr << texts[i].left(candidate).toStdString() << "|" << texts[i].right(texts[i].length() - candidate).toStdString() << std::endl; +// } + + return candidate; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/textabbrev.h Thu Nov 11 22:04:59 2010 +0000 @@ -0,0 +1,108 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + Sonic Visualiser + An audio file viewer and annotation editor. + Centre for Digital Music, Queen Mary, University of London. + This file copyright 2006-2007 Chris Cannam and QMUL. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. See the file + COPYING included with this distribution for more information. +*/ + +#ifndef _TEXT_ABBREV_H_ +#define _TEXT_ABBREV_H_ + +#include <QString> +#include <QStringList> + +class QFontMetrics; + +class TextAbbrev +{ +public: + enum Policy { + ElideEnd, + ElideEndAndCommonPrefixes, + ElideStart, + ElideMiddle + }; + + /** + * Abbreviate the given text to the given maximum length + * (including ellipsis), using the given abbreviation policy. If + * fuzzy is true, the text will be left alone if it is "not much + * more than" the maximum length. + * + * If ellipsis is non-empty, it will be used to show elisions in + * preference to the default (which is "..."). + */ + static QString abbreviate(QString text, int maxLength, + Policy policy = ElideEnd, + bool fuzzy = true, + QString ellipsis = ""); + + /** + * Abbreviate the given text to the given maximum painted width, + * using the given abbreviation policy. maxWidth is also modified + * so as to return the painted width of the abbreviated text. + * + * If ellipsis is non-empty, it will be used to show elisions in + * preference to the default (which is tr("...")). + */ + static QString abbreviate(QString text, + const QFontMetrics &metrics, + int &maxWidth, + Policy policy = ElideEnd, + QString ellipsis = ""); + + /** + * Abbreviate all of the given texts to the given maximum length, + * using the given abbreviation policy. If fuzzy is true, texts + * that are "not much more than" the maximum length will be left + * alone. + * + * If ellipsis is non-empty, it will be used to show elisions in + * preference to the default (which is tr("...")). + */ + static QStringList abbreviate(const QStringList &texts, int maxLength, + Policy policy = ElideEndAndCommonPrefixes, + bool fuzzy = true, + QString ellipsis = ""); + + /** + * Abbreviate all of the given texts to the given maximum painted + * width, using the given abbreviation policy. maxWidth is also + * modified so as to return the maximum painted width of the + * abbreviated texts. + * + * If ellipsis is non-empty, it will be used to show elisions in + * preference to the default (which is tr("...")). + */ + static QStringList abbreviate(const QStringList &texts, + const QFontMetrics &metrics, + int &maxWidth, + Policy policy = ElideEndAndCommonPrefixes, + QString ellipsis = ""); + +protected: + static QString getDefaultEllipsis(); + static int getFuzzLength(QString ellipsis); + static int getFuzzWidth(const QFontMetrics &metrics, QString ellipsis); + static QString abbreviateTo(QString text, int characters, + Policy policy, QString ellipsis); + static QStringList elidePrefixes(const QStringList &texts, + int targetReduction, + QString ellipsis); + static QStringList elidePrefixes(const QStringList &texts, + const QFontMetrics &metrics, + int targetWidthReduction, + QString ellipsis); + static int getPrefixLength(const QStringList &texts); +}; + +#endif +