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