X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fqtui%2Fsettingspages%2Faliasesmodel.cpp;h=780b152a672edd40f0d888d3a202ca292536d41b;hp=39b475ab0e53b89b1399811bbe00174a1b5a83b2;hb=673ded0d543cbdc2cf6e746b6bee7c1d21af8f90;hpb=0a43227b8cd44625f4881cc1545d42c8c8a4876c diff --git a/src/qtui/settingspages/aliasesmodel.cpp b/src/qtui/settingspages/aliasesmodel.cpp index 39b475ab..780b152a 100644 --- a/src/qtui/settingspages/aliasesmodel.cpp +++ b/src/qtui/settingspages/aliasesmodel.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2005-2016 by the Quassel Project * + * Copyright (C) 2005-2020 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -26,14 +26,12 @@ #include "client.h" #include "signalproxy.h" -AliasesModel::AliasesModel(QObject *parent) - : QAbstractItemModel(parent), - _configChanged(false), - _modelReady(false) +AliasesModel::AliasesModel(QObject* parent) + : QAbstractItemModel(parent) { // we need this signal for future connects to reset the data; - connect(Client::instance(), SIGNAL(connected()), this, SLOT(clientConnected())); - connect(Client::instance(), SIGNAL(disconnected()), this, SLOT(clientDisconnected())); + connect(Client::instance(), &Client::connected, this, &AliasesModel::clientConnected); + connect(Client::instance(), &Client::disconnected, this, &AliasesModel::clientDisconnected); if (Client::isConnected()) clientConnected(); @@ -41,8 +39,7 @@ AliasesModel::AliasesModel(QObject *parent) emit modelReady(false); } - -QVariant AliasesModel::data(const QModelIndex &index, int role) const +QVariant AliasesModel::data(const QModelIndex& index, int role) const { if (!_modelReady) return QVariant(); @@ -57,18 +54,70 @@ QVariant AliasesModel::data(const QModelIndex &index, int role) const return tr("The shortcut for the alias
" "It can be used as a regular slash command.

" "Example: \"foo\" can be used per /foo"); - case 1: - return tr("The string the shortcut will be expanded to
" - "special variables:
" - " - $i represents the i'th parameter.
" - " - $i..j represents the i'th to j'th parameter separated by spaces.
" - " - $i.. represents all parameters from i on separated by spaces.
" - " - $i:hostname represents the hostname of the user identified by the i'th parameter or a * if unknown.
" - " - $0 the whole string.
" - " - $nick your current nickname
" - " - $channel the name of the selected channel

" - "Multiple commands can be separated with semicolons

" - "Example: \"Test $1; Test $2; Test All $0\" will be expanded to three separate messages \"Test 1\", \"Test 2\" and \"Test All 1 2 3\" when called like /test 1 2 3"); + case 1: { + // To avoid overwhelming the user, organize things into a table + QString strTooltip; + QTextStream tooltip(&strTooltip, QIODevice::WriteOnly); + tooltip << ""; + + // Function to add a row to the tooltip table + auto addRow = [&](const QString& key, const QString& value = QString(), bool condition = true) { + if (condition) { + if (value.isEmpty()) { + tooltip << "" << key << ""; + } + else { + tooltip << "" << key << "" << value << ""; + } + } + }; + + tooltip << "

" << tr("The string the shortcut will be expanded to") << "

"; + + tooltip << "

" << tr("Special variables") << "

"; + + // Variable option table + tooltip << ""; + + // Parameter variables + addRow(tr("Parameter variables")); + addRow("$i", tr("i'th parameter")); + addRow("$i..j", tr("i'th to j'th parameter separated by spaces")); + addRow("$i..", tr("all parameters from i on separated by spaces")); + + // IrcUser handling + addRow(tr("Nickname parameter variables")); + addRow("$i:account", + tr("account of user identified by i'th parameter, or a '*' if logged out or " + "unknown")); + addRow("$i:hostname", tr("hostname of user identified by i'th parameter, or a '*' if unknown")); + addRow("$i:ident", tr("ident of user identified by i'th parameter, or a '*' if unknown")); + addRow("$i:identd", + tr("ident of user identified by i'th parameter if verified, or a '*' if unknown " + "or unverified (prefixed with '~')")); + + // General variables + addRow(tr("General variables")); + addRow("$0", tr("the whole string")); + addRow("$nick", tr("your current nickname")); + addRow("$channel", tr("the name of the selected channel")); + + // End table + tooltip << "
"; + + // Example header + tooltip << "

" << tr("Multiple commands can be separated with semicolons") << "

"; + // Example + tooltip << "

"; + tooltip << QString("

%1 %2
").arg(tr("Example:"), tr("\"Test $1; Test $2; Test All $0\"")); + tooltip << tr("...will be expanded to three separate messages \"Test 1\", \"Test 2\" " + "and \"Test All 1 2 3\" when called like /test 1 2 3") + << "

"; + + // End tooltip + tooltip << "
"; + return strTooltip; + } default: return QVariant(); } @@ -87,8 +136,7 @@ QVariant AliasesModel::data(const QModelIndex &index, int role) const } } - -bool AliasesModel::setData(const QModelIndex &index, const QVariant &value, int role) +bool AliasesModel::setData(const QModelIndex& index, const QVariant& value, int role) { if (!_modelReady) return false; @@ -117,12 +165,11 @@ bool AliasesModel::setData(const QModelIndex &index, const QVariant &value, int } } - void AliasesModel::newAlias() { QString newName("alias"); int i = 0; - AliasManager &manager = cloneAliasManager(); + AliasManager& manager = cloneAliasManager(); while (manager.contains(newName)) { i++; newName = QString("alias%1").arg(i); @@ -132,13 +179,12 @@ void AliasesModel::newAlias() endInsertRows(); } - void AliasesModel::loadDefaults() { if (!_modelReady) return; - AliasManager &manager = cloneAliasManager(); + AliasManager& manager = cloneAliasManager(); if (!manager.isEmpty()) { beginRemoveRows(QModelIndex(), 0, rowCount() - 1); @@ -149,26 +195,24 @@ void AliasesModel::loadDefaults() AliasManager::AliasList defaults = AliasManager::defaults(); beginInsertRows(QModelIndex(), 0, defaults.count() - 1); - foreach(AliasManager::Alias alias, defaults) { + foreach (AliasManager::Alias alias, defaults) { manager.addAlias(alias.name, alias.expansion); } endInsertRows(); } - void AliasesModel::removeAlias(int index) { if (index < 0 || index >= rowCount()) return; - AliasManager &manager = cloneAliasManager(); + AliasManager& manager = cloneAliasManager(); beginRemoveRows(QModelIndex(), index, index); manager.removeAt(index); endRemoveRows(); } - -Qt::ItemFlags AliasesModel::flags(const QModelIndex &index) const +Qt::ItemFlags AliasesModel::flags(const QModelIndex& index) const { if (!index.isValid()) { return Qt::ItemIsDropEnabled; @@ -178,12 +222,10 @@ Qt::ItemFlags AliasesModel::flags(const QModelIndex &index) const } } - QVariant AliasesModel::headerData(int section, Qt::Orientation orientation, int role) const { QStringList header; - header << tr("Alias") - << tr("Expansion"); + header << tr("Alias") << tr("Expansion"); if (orientation == Qt::Horizontal && role == Qt::DisplayRole) return header[section]; @@ -191,68 +233,55 @@ QVariant AliasesModel::headerData(int section, Qt::Orientation orientation, int return QVariant(); } - -QModelIndex AliasesModel::index(int row, int column, const QModelIndex &parent) const +QModelIndex AliasesModel::index(int row, int column, const QModelIndex& parent) const { Q_UNUSED(parent); if (row >= rowCount() || column >= columnCount()) - return QModelIndex(); + return {}; return createIndex(row, column); } - -const AliasManager &AliasesModel::aliasManager() const +const AliasManager& AliasesModel::aliasManager() const { - if (_configChanged) - return _clonedAliasManager; - else - return *Client::aliasManager(); + return _clonedAliasManager ? *_clonedAliasManager : *Client::aliasManager(); } - -AliasManager &AliasesModel::aliasManager() +AliasManager& AliasesModel::aliasManager() { - if (_configChanged) - return _clonedAliasManager; - else - return *Client::aliasManager(); + return _clonedAliasManager ? *_clonedAliasManager : *Client::aliasManager(); } - -AliasManager &AliasesModel::cloneAliasManager() +AliasManager& AliasesModel::cloneAliasManager() { - if (!_configChanged) { - _clonedAliasManager = *Client::aliasManager(); - _configChanged = true; + if (!_clonedAliasManager) { + _clonedAliasManager = std::make_unique(); + _clonedAliasManager->fromVariantMap(Client::aliasManager()->toVariantMap()); emit configChanged(true); } - return _clonedAliasManager; + return *_clonedAliasManager; } - void AliasesModel::revert() { - if (!_configChanged) + if (!_clonedAliasManager) return; - _configChanged = false; - emit configChanged(false); beginResetModel(); + _clonedAliasManager.reset(); endResetModel(); + emit configChanged(false); } - void AliasesModel::commit() { - if (!_configChanged) + if (!_clonedAliasManager) return; - Client::aliasManager()->requestUpdate(_clonedAliasManager.toVariantMap()); + Client::aliasManager()->requestUpdate(_clonedAliasManager->toVariantMap()); revert(); } - void AliasesModel::initDone() { _modelReady = true; @@ -261,23 +290,20 @@ void AliasesModel::initDone() emit modelReady(true); } - void AliasesModel::clientConnected() { - connect(Client::aliasManager(), SIGNAL(updated()), SLOT(revert())); + connect(Client::aliasManager(), &AliasManager::updated, this, &AliasesModel::revert); if (Client::aliasManager()->isInitialized()) initDone(); else - connect(Client::aliasManager(), SIGNAL(initDone()), SLOT(initDone())); + connect(Client::aliasManager(), &SyncableObject::initDone, this, &AliasesModel::initDone); } - void AliasesModel::clientDisconnected() { - // clear - _clonedAliasManager = ClientAliasManager(); _modelReady = false; beginResetModel(); + _clonedAliasManager.reset(); endResetModel(); emit modelReady(false); }