Chris@376: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@376: Chris@376: /* Chris@376: Sonic Visualiser Chris@376: An audio file viewer and annotation editor. Chris@376: Centre for Digital Music, Queen Mary, University of London. Chris@376: This file copyright 2006-2007 Chris Cannam and QMUL. Chris@376: Chris@376: This program is free software; you can redistribute it and/or Chris@376: modify it under the terms of the GNU General Public License as Chris@376: published by the Free Software Foundation; either version 2 of the Chris@376: License, or (at your option) any later version. See the file Chris@376: COPYING included with this distribution for more information. Chris@376: */ Chris@376: Chris@376: #ifndef _TEXT_ABBREV_H_ Chris@376: #define _TEXT_ABBREV_H_ Chris@376: Chris@376: #include Chris@376: #include Chris@376: Chris@376: class QFontMetrics; Chris@376: Chris@376: class TextAbbrev Chris@376: { Chris@376: public: Chris@376: enum Policy { Chris@376: ElideEnd, Chris@376: ElideEndAndCommonPrefixes, Chris@376: ElideStart, Chris@376: ElideMiddle Chris@376: }; Chris@376: Chris@376: /** Chris@376: * Abbreviate the given text to the given maximum length Chris@376: * (including ellipsis), using the given abbreviation policy. If Chris@376: * fuzzy is true, the text will be left alone if it is "not much Chris@376: * more than" the maximum length. Chris@376: * Chris@376: * If ellipsis is non-empty, it will be used to show elisions in Chris@376: * preference to the default (which is "..."). Chris@376: */ Chris@376: static QString abbreviate(QString text, int maxLength, Chris@376: Policy policy = ElideEnd, Chris@376: bool fuzzy = true, Chris@376: QString ellipsis = ""); Chris@376: Chris@376: /** Chris@376: * Abbreviate the given text to the given maximum painted width, Chris@376: * using the given abbreviation policy. maxWidth is also modified Chris@376: * so as to return the painted width of the abbreviated text. Chris@376: * Chris@376: * If ellipsis is non-empty, it will be used to show elisions in Chris@376: * preference to the default (which is tr("...")). Chris@376: */ Chris@376: static QString abbreviate(QString text, Chris@376: const QFontMetrics &metrics, Chris@376: int &maxWidth, Chris@376: Policy policy = ElideEnd, Chris@376: QString ellipsis = ""); Chris@376: Chris@376: /** Chris@376: * Abbreviate all of the given texts to the given maximum length, Chris@376: * using the given abbreviation policy. If fuzzy is true, texts Chris@376: * that are "not much more than" the maximum length will be left Chris@376: * alone. Chris@376: * Chris@376: * If ellipsis is non-empty, it will be used to show elisions in Chris@376: * preference to the default (which is tr("...")). Chris@376: */ Chris@376: static QStringList abbreviate(const QStringList &texts, int maxLength, Chris@376: Policy policy = ElideEndAndCommonPrefixes, Chris@376: bool fuzzy = true, Chris@376: QString ellipsis = ""); Chris@376: Chris@376: /** Chris@376: * Abbreviate all of the given texts to the given maximum painted Chris@376: * width, using the given abbreviation policy. maxWidth is also Chris@376: * modified so as to return the maximum painted width of the Chris@376: * abbreviated texts. Chris@376: * Chris@376: * If ellipsis is non-empty, it will be used to show elisions in Chris@376: * preference to the default (which is tr("...")). Chris@376: */ Chris@376: static QStringList abbreviate(const QStringList &texts, Chris@376: const QFontMetrics &metrics, Chris@376: int &maxWidth, Chris@376: Policy policy = ElideEndAndCommonPrefixes, Chris@376: QString ellipsis = ""); Chris@376: Chris@376: protected: Chris@376: static QString getDefaultEllipsis(); Chris@376: static int getFuzzLength(QString ellipsis); Chris@376: static int getFuzzWidth(const QFontMetrics &metrics, QString ellipsis); Chris@376: static QString abbreviateTo(QString text, int characters, Chris@376: Policy policy, QString ellipsis); Chris@376: static QStringList elidePrefixes(const QStringList &texts, Chris@376: int targetReduction, Chris@376: QString ellipsis); Chris@376: static QStringList elidePrefixes(const QStringList &texts, Chris@376: const QFontMetrics &metrics, Chris@376: int targetWidthReduction, Chris@376: QString ellipsis); Chris@376: static int getPrefixLength(const QStringList &texts); Chris@376: }; Chris@376: Chris@376: #endif Chris@376: