CommandHelpButton::CommandHelpButton(QWidget *parent) : QWidget(parent) , m_button(new QToolButton(this)) , m_help(NULL) { m_button->setToolTip( tr("Show command help (F1)") ); m_button->setShortcut(QKeySequence(Qt::Key_F1)); const int x = smallIconSize(); m_button->setIconSize(QSize(x, x)); m_button->setIcon( getIcon("help-faq", IconInfoSign) ); connect( m_button, SIGNAL(clicked()), this, SLOT(showHelp()) ); QVBoxLayout *layout = createLayout(this); layout->addWidget(m_button); }
QSize Utils::Gui::mediumIconSize(const QWidget *widget) { // under a 1080p screen is usually 24x24 return ((smallIconSize(widget) + largeIconSize(widget)) / 2); }
QString elideText(const QString &text, const QFont &font, const QString &format, bool escapeAmpersands, int maxWidthPixels, int maxLines) { if (maxWidthPixels <= 0) maxWidthPixels = smallIconSize() * 20; QStringList lines = text.split('\n'); // Ignore empty lines at beginning. static const QRegExp reNonEmpty(".*\\S.*"); const int firstLine = qMax(0, lines.indexOf(reNonEmpty)); const int lastLine = qMax(0, lines.lastIndexOf(reNonEmpty, firstLine + maxLines - 1)); // If empty lines are at beginning, prepend triple dot. if (firstLine != 0) lines[firstLine].prepend("..."); // If there are too many lines, append triple dot. if (lastLine + 1 != lines.size()) lines[lastLine].append("..."); lines = lines.mid(firstLine, lastLine - firstLine + 1); QFontMetrics fm(font); const int formatWidth = format.isEmpty() ? 0 : fm.width(format.arg(QString())); // Remove redundant spaces from single line text. if (lines.size() == 1) lines[0] = lines[0].simplified(); // Find common indentation. int commonIndent = lines.value(0).size(); static const QRegExp reNonSpace("\\S"); for (const auto &line : lines) { const int lineIndent = line.indexOf(reNonSpace); if (lineIndent != -1 && lineIndent < commonIndent) { commonIndent = lineIndent; if (commonIndent == 0) break; } } // Remove common indentation each line and elide text if too long. for (auto &line : lines) { line = line.mid(commonIndent); // Make eliding huge text faster. if (line.size() > maxElidedTextLineLength) line = line.left(maxElidedTextLineLength) + "..."; line = fm.elidedText(line, Qt::ElideMiddle, maxWidthPixels - formatWidth); } QString result = lines.join("\n"); // Escape all ampersands. if (escapeAmpersands) result.replace( QChar('&'), QString("&&") ); return format.isEmpty() ? result : format.arg(result); }