annotate src/textabbrev.h @ 518:5c846f3c9244

Fix inaccurate and frightening text
author Chris Cannam
date Thu, 20 Oct 2011 15:39:46 +0100
parents b9c153e00e84
children 533519ebc0cb
rev   line source
Chris@50 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@50 2
Chris@50 3 /*
Chris@57 4 EasyMercurial
Chris@57 5
Chris@57 6 Based on HgExplorer by Jari Korhonen
Chris@57 7 Copyright (c) 2010 Jari Korhonen
Chris@244 8 Copyright (c) 2011 Chris Cannam
Chris@244 9 Copyright (c) 2011 Queen Mary, University of London
Chris@50 10
Chris@50 11 This program is free software; you can redistribute it and/or
Chris@50 12 modify it under the terms of the GNU General Public License as
Chris@50 13 published by the Free Software Foundation; either version 2 of the
Chris@50 14 License, or (at your option) any later version. See the file
Chris@50 15 COPYING included with this distribution for more information.
Chris@50 16 */
Chris@50 17
Chris@50 18 #ifndef _TEXT_ABBREV_H_
Chris@50 19 #define _TEXT_ABBREV_H_
Chris@50 20
Chris@50 21 #include <QString>
Chris@50 22 #include <QStringList>
Chris@50 23
Chris@50 24 class QFontMetrics;
Chris@50 25
Chris@50 26 class TextAbbrev
Chris@50 27 {
Chris@50 28 public:
Chris@50 29 enum Policy {
Chris@50 30 ElideEnd,
Chris@50 31 ElideEndAndCommonPrefixes,
Chris@50 32 ElideStart,
Chris@50 33 ElideMiddle
Chris@50 34 };
Chris@50 35
Chris@50 36 /**
Chris@50 37 * Abbreviate the given text to the given maximum length
Chris@50 38 * (including ellipsis), using the given abbreviation policy. If
Chris@50 39 * fuzzy is true, the text will be left alone if it is "not much
Chris@50 40 * more than" the maximum length.
Chris@50 41 *
Chris@50 42 * If ellipsis is non-empty, it will be used to show elisions in
Chris@50 43 * preference to the default (which is "...").
Chris@50 44 */
Chris@50 45 static QString abbreviate(QString text, int maxLength,
Chris@50 46 Policy policy = ElideEnd,
Chris@50 47 bool fuzzy = true,
Chris@50 48 QString ellipsis = "");
Chris@50 49
Chris@50 50 /**
Chris@50 51 * Abbreviate the given text to the given maximum painted width,
Chris@50 52 * using the given abbreviation policy. maxWidth is also modified
Chris@50 53 * so as to return the painted width of the abbreviated text.
Chris@50 54 *
Chris@50 55 * If ellipsis is non-empty, it will be used to show elisions in
Chris@50 56 * preference to the default (which is tr("...")).
Chris@50 57 */
Chris@50 58 static QString abbreviate(QString text,
Chris@50 59 const QFontMetrics &metrics,
Chris@50 60 int &maxWidth,
Chris@50 61 Policy policy = ElideEnd,
Chris@53 62 QString ellipsis = "",
Chris@53 63 int wrapLines = 1);
Chris@50 64
Chris@50 65 /**
Chris@50 66 * Abbreviate all of the given texts to the given maximum length,
Chris@50 67 * using the given abbreviation policy. If fuzzy is true, texts
Chris@50 68 * that are "not much more than" the maximum length will be left
Chris@50 69 * alone.
Chris@50 70 *
Chris@50 71 * If ellipsis is non-empty, it will be used to show elisions in
Chris@50 72 * preference to the default (which is tr("...")).
Chris@50 73 */
Chris@50 74 static QStringList abbreviate(const QStringList &texts, int maxLength,
Chris@50 75 Policy policy = ElideEndAndCommonPrefixes,
Chris@50 76 bool fuzzy = true,
Chris@50 77 QString ellipsis = "");
Chris@50 78
Chris@50 79 /**
Chris@50 80 * Abbreviate all of the given texts to the given maximum painted
Chris@50 81 * width, using the given abbreviation policy. maxWidth is also
Chris@50 82 * modified so as to return the maximum painted width of the
Chris@50 83 * abbreviated texts.
Chris@50 84 *
Chris@50 85 * If ellipsis is non-empty, it will be used to show elisions in
Chris@50 86 * preference to the default (which is tr("...")).
Chris@50 87 */
Chris@50 88 static QStringList abbreviate(const QStringList &texts,
Chris@50 89 const QFontMetrics &metrics,
Chris@50 90 int &maxWidth,
Chris@50 91 Policy policy = ElideEndAndCommonPrefixes,
Chris@50 92 QString ellipsis = "");
Chris@50 93
Chris@50 94 protected:
Chris@50 95 static QString getDefaultEllipsis();
Chris@50 96 static int getFuzzLength(QString ellipsis);
Chris@50 97 static int getFuzzWidth(const QFontMetrics &metrics, QString ellipsis);
Chris@50 98 static QString abbreviateTo(QString text, int characters,
Chris@50 99 Policy policy, QString ellipsis);
Chris@50 100 static QStringList elidePrefixes(const QStringList &texts,
Chris@50 101 int targetReduction,
Chris@50 102 QString ellipsis);
Chris@50 103 static QStringList elidePrefixes(const QStringList &texts,
Chris@50 104 const QFontMetrics &metrics,
Chris@50 105 int targetWidthReduction,
Chris@50 106 QString ellipsis);
Chris@50 107 static int getPrefixLength(const QStringList &texts);
Chris@50 108 };
Chris@50 109
Chris@50 110 #endif
Chris@50 111