modernize: Prefer default member init over ctor init
[quassel.git] / src / qtui / settingspages / aliasesmodel.cpp
index 4d00d37..39f2877 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-09 by the Quassel Project                          *
+ *   Copyright (C) 2005-2018 by the Quassel Project                        *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
@@ -15,7 +15,7 @@
  *   You should have received a copy of the GNU General Public License     *
  *   along with this program; if not, write to the                         *
  *   Free Software Foundation, Inc.,                                       *
- *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
  ***************************************************************************/
 
 #include "aliasesmodel.h"
 #include "signalproxy.h"
 
 AliasesModel::AliasesModel(QObject *parent)
-  : QAbstractItemModel(parent),
-    _configChanged(false),
-    _modelReady(false)
+    : 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()));
+    // 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()));
+
+    if (Client::isConnected())
+        clientConnected();
+    else
+        emit modelReady(false);
+}
 
-  if(Client::isConnected())
-    clientConnected();
-  else
-    emit modelReady(false);
+
+QVariant AliasesModel::data(const QModelIndex &index, int role) const
+{
+    if (!_modelReady)
+        return QVariant();
+
+    if (!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount())
+        return QVariant();
+
+    switch (role) {
+    case Qt::ToolTipRole:
+        switch (index.column()) {
+        case 0:
+            return tr("<b>The shortcut for the alias</b><br />"
+                      "It can be used as a regular slash command.<br /><br />"
+                      "<b>Example:</b> \"foo\" can be used per /foo");
+        case 1:
+        {
+            // To avoid overwhelming the user, organize things into a table
+            QString strTooltip;
+            QTextStream tooltip( &strTooltip, QIODevice::WriteOnly );
+            tooltip << "<qt><style>.bold { font-weight: bold; } .italic { font-style: italic; }</style>";
+
+            // 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 << "<tr><td class='italic' align='left' colspan='2'>"
+                                      << key << "</td></tr>";
+                    } else {
+                        tooltip << "<tr><td class='bold' align='left'>"
+                                      << key << "</td><td>" << value << "</td></tr>";
+                    }
+                }
+            };
+
+            tooltip << "<p class='bold'>"
+                    << tr("The string the shortcut will be expanded to") << "</p>";
+
+            tooltip << "<p class='bold' align='center'>"
+                    << tr("Special variables") << "</p>";
+
+            // Variable option table
+            tooltip << "<table cellspacing='5' cellpadding='0'>";
+
+            // 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 << "</table>";
+
+            // Example header
+            tooltip << "<p>"
+                    << tr("Multiple commands can be separated with semicolons") << "</p>";
+            // Example
+            tooltip << "<p>";
+            tooltip << QString("<p><span class='bold'>%1</span> %2<br />").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 <i>/test 1 2 3</i>")
+                    << "</p>";
+
+            // End tooltip
+            tooltip << "</qt>";
+            return strTooltip;
+        }
+        default:
+            return QVariant();
+        }
+    case Qt::DisplayRole:
+    case Qt::EditRole:
+        switch (index.column()) {
+        case 0:
+            return aliasManager()[index.row()].name;
+        case 1:
+            return aliasManager()[index.row()].expansion;
+        default:
+            return QVariant();
+        }
+    default:
+        return QVariant();
+    }
 }
 
-QVariant AliasesModel::data(const QModelIndex &index, int role) const {
-  if(!_modelReady)
-    return QVariant();
 
-  if(!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount())
-    return QVariant();
+bool AliasesModel::setData(const QModelIndex &index, const QVariant &value, int role)
+{
+    if (!_modelReady)
+        return false;
+
+    if (!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount() || role != Qt::EditRole)
+        return false;
+
+    QString newValue = value.toString();
+    if (newValue.isEmpty())
+        return false;
 
-  switch(role) {
-  case Qt::ToolTipRole:
-    switch(index.column()) {
+    switch (index.column()) {
     case 0:
-      return tr("<b>The shortcut for the alias</b><br />"
-       "It can be used as a regular slash command.<br /><br />"
-       "<b>Example:</b> \"foo\" can be used per /foo");
+        if (aliasManager().contains(newValue)) {
+            return false;
+        }
+        else {
+            cloneAliasManager()[index.row()].name = newValue;
+            return true;
+        }
     case 1:
-      return tr("<b>The string the shortcut will be expanded to</b><br />"
-       "<b>special variables:</b><br />"
-       " - <b>$i</b> represents the i'th parameter.<br />"
-       " - <b>$i..j</b> represents the i'th to j'th parameter separated by spaces.<br />"
-       " - <b>$i..</b> represents all parameters from i on separated by spaces.<br />"
-       " - <b>$i:hostname</b> represents the hostname of the user identified by the i'th parameter or a * if unknown.<br />"
-       " - <b>$0</b> the whole string.<br />"
-       " - <b>$nick</b> your current nickname<br />"
-       " - <b>$channel</b> the name of the selected channel<br /><br />"
-       "Multiple commands can be separated with semicolons<br /><br />"
-       "<b>Example:</b> \"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");
+        cloneAliasManager()[index.row()].expansion = newValue;
+        return true;
     default:
-      return QVariant();
+        return false;
     }
-  case Qt::DisplayRole:
-  case Qt::EditRole:
-    switch(index.column()) {
-    case 0:
-      return aliasManager()[index.row()].name;
-    case 1:
-      return aliasManager()[index.row()].expansion;
-    default:
-      return QVariant();
+}
+
+
+void AliasesModel::newAlias()
+{
+    QString newName("alias");
+    int i = 0;
+    AliasManager &manager = cloneAliasManager();
+    while (manager.contains(newName)) {
+        i++;
+        newName = QString("alias%1").arg(i);
     }
-  default:
-    return QVariant();
-  }
+    beginInsertRows(QModelIndex(), rowCount(), rowCount());
+    manager.addAlias(newName, "Expansion");
+    endInsertRows();
 }
 
-bool AliasesModel::setData(const QModelIndex &index, const QVariant &value, int role) {
-  if(!_modelReady)
-    return false;
 
-  if(!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount() || role != Qt::EditRole)
-    return false;
+void AliasesModel::loadDefaults()
+{
+    if (!_modelReady)
+        return;
 
-  QString newValue = value.toString();
-  if(newValue.isEmpty())
-    return false;
+    AliasManager &manager = cloneAliasManager();
 
-  switch(index.column()) {
-  case 0:
-    if(aliasManager().contains(newValue)) {
-      return false;
-    } else {
-      cloneAliasManager()[index.row()].name = newValue;
-      return true;
+    if (!manager.isEmpty()) {
+        beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
+        for (int i = rowCount() - 1; i >= 0; i--)
+            manager.removeAt(i);
+        endRemoveRows();
     }
-  case 1:
-    cloneAliasManager()[index.row()].expansion = newValue;
-    return true;
-  default:
-    return false;
-  }
-}
-
-void AliasesModel::newAlias() {
-  QString newName("alias");
-  int i = 0;
-  AliasManager &manager = cloneAliasManager();
-  while(manager.contains(newName)) {
-    i++;
-    newName = QString("alias%1").arg(i);
-  }
-  beginInsertRows(QModelIndex(), rowCount(), rowCount());
-  manager.addAlias(newName, "Expansion");
-  endInsertRows();
-}
-
-void AliasesModel::loadDefaults() {
-  if(!_modelReady)
-    return;
-
-  AliasManager &manager = cloneAliasManager();
-
-  if(!manager.isEmpty()) {
-    beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
-    for(int i = rowCount() - 1; i >= 0; i--)
-      manager.removeAt(i);
-    endRemoveRows();
-  }
 
-  AliasManager::AliasList defaults = AliasManager::defaults();
-  beginInsertRows(QModelIndex(), 0, defaults.count() - 1);
-  foreach(AliasManager::Alias alias, defaults) {
-    manager.addAlias(alias.name, alias.expansion);
-  }
-  endInsertRows();
+    AliasManager::AliasList defaults = AliasManager::defaults();
+    beginInsertRows(QModelIndex(), 0, defaults.count() - 1);
+    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();  
-  beginRemoveRows(QModelIndex(), index, index);
-  manager.removeAt(index);
-  endRemoveRows();
+void AliasesModel::removeAlias(int index)
+{
+    if (index < 0 || index >= rowCount())
+        return;
+
+    AliasManager &manager = cloneAliasManager();
+    beginRemoveRows(QModelIndex(), index, index);
+    manager.removeAt(index);
+    endRemoveRows();
 }
 
-Qt::ItemFlags AliasesModel::flags(const QModelIndex &index) const {
-  if(!index.isValid()) {
-    return Qt::ItemIsDropEnabled;
-  } else {
-    return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
-  }
+
+Qt::ItemFlags AliasesModel::flags(const QModelIndex &index) const
+{
+    if (!index.isValid()) {
+        return Qt::ItemIsDropEnabled;
+    }
+    else {
+        return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
+    }
 }
 
 
-QVariant AliasesModel::headerData(int section, Qt::Orientation orientation, int role) const {
-  QStringList header;
-  header << tr("Alias")
-         << tr("Expansion");
+QVariant AliasesModel::headerData(int section, Qt::Orientation orientation, int role) const
+{
+    QStringList header;
+    header << tr("Alias")
+           << tr("Expansion");
 
-  if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
-    return header[section];
+    if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
+        return header[section];
 
-  return QVariant();
+    return QVariant();
 }
 
-QModelIndex AliasesModel::index(int row, int column, const QModelIndex &parent) const {
-  Q_UNUSED(parent);
-  if(row >= rowCount() || column >= columnCount())
-    return QModelIndex();
 
-  return createIndex(row, column);
+QModelIndex AliasesModel::index(int row, int column, const QModelIndex &parent) const
+{
+    Q_UNUSED(parent);
+    if (row >= rowCount() || column >= columnCount())
+        return QModelIndex();
+
+    return createIndex(row, column);
 }
 
 
-const AliasManager &AliasesModel::aliasManager() const {
-  if(_configChanged)
-    return _clonedAliasManager;
-  else
-    return *Client::aliasManager();
+const AliasManager &AliasesModel::aliasManager() const
+{
+    if (_configChanged)
+        return _clonedAliasManager;
+    else
+        return *Client::aliasManager();
 }
 
-AliasManager &AliasesModel::aliasManager() {
-  if(_configChanged)
-    return _clonedAliasManager;
-  else
-    return *Client::aliasManager();
+
+AliasManager &AliasesModel::aliasManager()
+{
+    if (_configChanged)
+        return _clonedAliasManager;
+    else
+        return *Client::aliasManager();
 }
 
-AliasManager &AliasesModel::cloneAliasManager() {
-  if(!_configChanged) {
-    _clonedAliasManager = *Client::aliasManager();
-    _configChanged = true;
-    emit configChanged(true);
-  }
-  return _clonedAliasManager;
+
+AliasManager &AliasesModel::cloneAliasManager()
+{
+    if (!_configChanged) {
+        _clonedAliasManager = *Client::aliasManager();
+        _configChanged = true;
+        emit configChanged(true);
+    }
+    return _clonedAliasManager;
 }
 
-void AliasesModel::revert() {
-  if(!_configChanged)
-    return;
 
-  _configChanged = false;
-  emit configChanged(false);
-  reset();
+void AliasesModel::revert()
+{
+    if (!_configChanged)
+        return;
+
+    _configChanged = false;
+    emit configChanged(false);
+    beginResetModel();
+    endResetModel();
 }
 
-void AliasesModel::commit() {
-  if(!_configChanged)
-    return;
 
-  Client::aliasManager()->requestUpdate(_clonedAliasManager.toVariantMap());
-  revert();
+void AliasesModel::commit()
+{
+    if (!_configChanged)
+        return;
+
+    Client::aliasManager()->requestUpdate(_clonedAliasManager.toVariantMap());
+    revert();
 }
 
-void AliasesModel::initDone() {
-  _modelReady = true;
-  reset();
-  emit modelReady(true);
+
+void AliasesModel::initDone()
+{
+    _modelReady = true;
+    beginResetModel();
+    endResetModel();
+    emit modelReady(true);
 }
 
-void AliasesModel::clientConnected() {
-  connect(Client::aliasManager(), SIGNAL(updated()), SLOT(revert()));
-  if(Client::aliasManager()->isInitialized())
-    initDone();
-  else
-    connect(Client::aliasManager(), SIGNAL(initDone()), SLOT(initDone()));
+
+void AliasesModel::clientConnected()
+{
+    connect(Client::aliasManager(), SIGNAL(updated()), SLOT(revert()));
+    if (Client::aliasManager()->isInitialized())
+        initDone();
+    else
+        connect(Client::aliasManager(), SIGNAL(initDone()), SLOT(initDone()));
 }
 
-void AliasesModel::clientDisconnected() {
-  // clear
-  _clonedAliasManager = ClientAliasManager();
-  _modelReady = false;
-  reset();
-  emit modelReady(false);
+
+void AliasesModel::clientDisconnected()
+{
+    // clear
+    _clonedAliasManager = ClientAliasManager();
+    _modelReady = false;
+    beginResetModel();
+    endResetModel();
+    emit modelReady(false);
 }