From 6579cd49c867ce3fb6c99127851a881ea82d1b1b Mon Sep 17 00:00:00 2001 From: Marcus Eggenberger Date: Tue, 22 Jul 2008 03:08:08 +0200 Subject: [PATCH] jussi01: can you spell aliases? --- src/common/CMakeLists.txt | 2 + src/common/aliasmanager.cpp | 78 +++++++ src/common/aliasmanager.h | 67 ++++++ src/core/CMakeLists.txt | 2 + src/core/basichandler.h | 3 + src/core/corealiasmanager.cpp | 58 +++++ src/core/corealiasmanager.h | 42 ++++ src/core/coresession.cpp | 10 +- src/core/coresession.h | 9 +- src/core/userinputhandler.cpp | 21 +- src/core/userinputhandler.h | 3 + src/qtui/mainwin.cpp | 2 + src/qtui/settingspages/aliasesmodel.cpp | 203 ++++++++++++++++++ src/qtui/settingspages/aliasesmodel.h | 75 +++++++ .../settingspages/aliasessettingspage.cpp | 66 ++++++ src/qtui/settingspages/aliasessettingspage.h | 55 +++++ src/qtui/settingspages/aliasessettingspage.ui | 70 ++++++ src/qtui/settingspages/settingspages.inc | 6 +- src/uisupport/settingspage.cpp | 38 +--- src/uisupport/settingspage.h | 105 +++++---- 20 files changed, 821 insertions(+), 94 deletions(-) create mode 100644 src/common/aliasmanager.cpp create mode 100644 src/common/aliasmanager.h create mode 100644 src/core/corealiasmanager.cpp create mode 100644 src/core/corealiasmanager.h create mode 100644 src/qtui/settingspages/aliasesmodel.cpp create mode 100644 src/qtui/settingspages/aliasesmodel.h create mode 100644 src/qtui/settingspages/aliasessettingspage.cpp create mode 100644 src/qtui/settingspages/aliasessettingspage.h create mode 100644 src/qtui/settingspages/aliasessettingspage.ui diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 765d7ba1..ddccfb9e 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -5,6 +5,7 @@ set(QT_USE_QTNETWORK 1) include(${QT_USE_FILE}) set(SOURCES + aliasmanager.cpp backlogmanager.cpp bufferinfo.cpp buffersyncer.cpp @@ -24,6 +25,7 @@ set(SOURCES cliparser.cpp) set(MOC_HDRS + aliasmanager.h backlogmanager.h buffersyncer.h bufferviewconfig.h diff --git a/src/common/aliasmanager.cpp b/src/common/aliasmanager.cpp new file mode 100644 index 00000000..c03c4e42 --- /dev/null +++ b/src/common/aliasmanager.cpp @@ -0,0 +1,78 @@ +/*************************************************************************** + * Copyright (C) 2005-08 by the Quassel Project * + * devel@quassel-irc.org * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) version 3. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * 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. * + ***************************************************************************/ + +#include "aliasmanager.h" + +#include +#include + +AliasManager &AliasManager::operator=(const AliasManager &other) { + _aliases = other._aliases; + return *this; +} + +int AliasManager::indexOf(const QString &name) const { + for(int i = 0; i < _aliases.count(); i++) { + if(_aliases[i].name == name) + return i; + } + return -1; +} + +QVariantMap AliasManager::initAliases() const { + QVariantMap aliases; + QStringList names; + QStringList expansions; + + for(int i = 0; i < _aliases.count(); i++) { + names << _aliases[i].name; + expansions << _aliases[i].expansion; + } + + aliases["names"] = names; + aliases["expansions"] = expansions; + return aliases; +} + +void AliasManager::initSetAliases(const QVariantMap &aliases) { + QStringList names = aliases["names"].toStringList(); + QStringList expansions = aliases["expansions"].toStringList(); + + if(names.count() != expansions.count()) { + qWarning() << "AliasesManager::initSetAliases: received" << names.count() << "alias names but only" << expansions.count() << "expansions!"; + return; + } + + _aliases.clear(); + for(int i = 0; i < names.count(); i++) { + _aliases << Alias(names[i], expansions[i]); + } +} + + +void AliasManager::addAlias(const QString &name, const QString &expansion) { + if(contains(name)) { + return; + } + + _aliases << Alias(name, expansion); + + emit aliasAdded(name, expansion); +} diff --git a/src/common/aliasmanager.h b/src/common/aliasmanager.h new file mode 100644 index 00000000..37d38804 --- /dev/null +++ b/src/common/aliasmanager.h @@ -0,0 +1,67 @@ +/*************************************************************************** + * Copyright (C) 2005-08 by the Quassel Project * + * devel@quassel-irc.org * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) version 3. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * 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. * + ***************************************************************************/ + +#ifndef ALIASMANAGER_H +#define ALIASMANAGER_H + +#include "syncableobject.h" + +#include + +class AliasManager : public SyncableObject { + Q_OBJECT + +public: + inline AliasManager(QObject *parent = 0) : SyncableObject(parent) { setAllowClientUpdates(true); } + AliasManager &operator=(const AliasManager &other); + + struct Alias { + QString name; + QString expansion; + Alias(const QString &name_, const QString &expansion_) : name(name_), expansion(expansion_) {} + }; + + int indexOf(const QString &name) const; + inline bool contains(const QString &name) const { return indexOf(name) != -1; } + inline bool isEmpty() const { return _aliases.isEmpty(); } + inline int count() const { return _aliases.count(); } + inline void removeAt(int index) { _aliases.removeAt(index); } + inline Alias &operator[](int i) { return _aliases[i]; } + inline const Alias &operator[](int i) const { return _aliases[i]; } + inline const QList &aliases() const { return _aliases; } + +public slots: + virtual QVariantMap initAliases() const; + virtual void initSetAliases(const QVariantMap &aliases); + + virtual void addAlias(const QString &name, const QString &expansion); + +protected: + void setAliases(const QList &aliases) { _aliases = aliases; } + +signals: + void aliasAdded(const QString &name, const QString &expansion); + +private: + QList _aliases; + +}; + +#endif //ALIASMANAGER_H diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index a7bbbd2f..d73be7c2 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -10,6 +10,7 @@ set(SOURCES abstractsqlstorage.cpp basichandler.cpp core.cpp + corealiasmanager.cpp corebacklogmanager.cpp corebufferviewconfig.cpp corebufferviewmanager.cpp @@ -31,6 +32,7 @@ set(MOC_HDRS abstractsqlstorage.h basichandler.h core.h + corealiasmanager.h corebacklogmanager.h corebufferviewconfig.h corebufferviewmanager.h diff --git a/src/core/basichandler.h b/src/core/basichandler.h index 47003478..c276c96c 100644 --- a/src/core/basichandler.h +++ b/src/core/basichandler.h @@ -31,6 +31,8 @@ #include "networkconnection.h" +class CoreSession; + class BasicHandler : public QObject { Q_OBJECT @@ -71,6 +73,7 @@ protected: inline Network *network() const { return _networkConnection->network(); } inline NetworkConnection *networkConnection() const { return _networkConnection; } + inline CoreSession *coreSession() const { return _networkConnection->coreSession(); } BufferInfo::Type typeByTarget(const QString &target) const; diff --git a/src/core/corealiasmanager.cpp b/src/core/corealiasmanager.cpp new file mode 100644 index 00000000..8fcdb022 --- /dev/null +++ b/src/core/corealiasmanager.cpp @@ -0,0 +1,58 @@ +/*************************************************************************** + * Copyright (C) 2005-08 by the Quassel Project * + * devel@quassel-irc.org * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) version 3. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * 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. * + ***************************************************************************/ + +#include "corealiasmanager.h" + +#include "core.h" +#include "coresession.h" + +CoreAliasManager::CoreAliasManager(CoreSession *parent) + : AliasManager(parent) +{ + CoreSession *session = qobject_cast(parent); + if(!session) { + qWarning() << "CoreAliasManager: unable to load Aliases. Parent is not a Coresession!"; + loadDefaults(); + return; + } + + QVariantMap aliases = Core::getUserSetting(session->user(), "Aliases").toMap(); + if(aliases.isEmpty()) { + loadDefaults(); + } else { + initSetAliases(aliases); + } +} + + +CoreAliasManager::~CoreAliasManager() { + CoreSession *session = qobject_cast(parent()); + if(!session) { + qWarning() << "CoreAliasManager: unable to save Aliases. Parent is not a Coresession!"; + return; + } + + Core::setUserSetting(session->user(), "Aliases", initAliases()); +} + +void CoreAliasManager::loadDefaults() { + // Default Aliases: + addAlias("j", "/join $1"); +} diff --git a/src/core/corealiasmanager.h b/src/core/corealiasmanager.h new file mode 100644 index 00000000..b5fe7612 --- /dev/null +++ b/src/core/corealiasmanager.h @@ -0,0 +1,42 @@ +/*************************************************************************** + * Copyright (C) 2005-08 by the Quassel Project * + * devel@quassel-irc.org * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) version 3. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * 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. * + ***************************************************************************/ + +#ifndef COREALIASMANAGER_H +#define COREALIASMANAGER_H + +#include "aliasmanager.h" + +class CoreSession; + +class CoreAliasManager : public AliasManager { + Q_OBJECT + +public: + CoreAliasManager(CoreSession *parent = 0); + ~CoreAliasManager(); + + inline virtual const QMetaObject *syncMetaObject() const { return &AliasManager::staticMetaObject; } + +private: + void loadDefaults(); + +}; + +#endif //COREALIASMANAGER_H diff --git a/src/core/coresession.cpp b/src/core/coresession.cpp index 2956d88c..696825d6 100644 --- a/src/core/coresession.cpp +++ b/src/core/coresession.cpp @@ -44,6 +44,7 @@ CoreSession::CoreSession(UserId uid, bool restoreState, QObject *parent) : QObject(parent), _user(uid), _signalProxy(new SignalProxy(SignalProxy::Server, 0, this)), + _aliasManager(this), _bufferSyncer(new BufferSyncer(this)), _backlogManager(new CoreBacklogManager(this)), _bufferViewManager(new CoreBufferViewManager(_signalProxy, this)), @@ -88,13 +89,16 @@ CoreSession::CoreSession(UserId uid, bool restoreState, QObject *parent) p->synchronize(_bufferSyncer); - // init BacklogManager; + // init alias manager + p->synchronize(&aliasManager()); + + // init BacklogManager p->synchronize(_backlogManager); - // init IrcListHelper; + // init IrcListHelper p->synchronize(ircListHelper()); - // init CoreInfo; + // init CoreInfo p->synchronize(&_coreInfo); // Restore session state diff --git a/src/core/coresession.h b/src/core/coresession.h index 7144fe2b..57c2a530 100644 --- a/src/core/coresession.h +++ b/src/core/coresession.h @@ -18,13 +18,14 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ -#ifndef _CORESESSION_H_ -#define _CORESESSION_H_ +#ifndef CORESESSION_H +#define CORESESSION_H #include #include #include "corecoreinfo.h" +#include "corealiasmanager.h" #include "message.h" class BufferSyncer; @@ -56,6 +57,9 @@ public: SignalProxy *signalProxy() const; + const AliasManager &aliasManager() const { return _aliasManager; } + AliasManager &aliasManager() { return _aliasManager; } + inline CoreIrcListHelper *ircListHelper() const { return _ircListHelper; } void attachNetworkConnection(NetworkConnection *conn); @@ -181,6 +185,7 @@ private: UserId _user; SignalProxy *_signalProxy; + CoreAliasManager _aliasManager; QHash _connections; QHash _networks; // QHash _networksToRemove; diff --git a/src/core/userinputhandler.cpp b/src/core/userinputhandler.cpp index 73bfcce1..e9b01399 100644 --- a/src/core/userinputhandler.cpp +++ b/src/core/userinputhandler.cpp @@ -349,8 +349,27 @@ void UserInputHandler::handleWhowas(const BufferInfo &bufferInfo, const QString } void UserInputHandler::defaultHandler(QString cmd, const BufferInfo &bufferInfo, const QString &msg) { - Q_UNUSED(bufferInfo) + for(int i = 0; i < coreSession()->aliasManager().count(); i++) { + if(coreSession()->aliasManager()[i].name.toLower() == cmd.toLower()) { + expand(coreSession()->aliasManager()[i].expansion, bufferInfo, msg); + return; + } + } emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: %1 %2").arg(cmd).arg(msg)); } +void UserInputHandler::expand(const QString &alias, const BufferInfo &bufferInfo, const QString &msg) { + QStringList commands = alias.split(QRegExp("; ?")); + QStringList params = msg.split(' '); + for(int i = 0; i < commands.count(); i++) { + QString command = commands[i]; + for(int j = params.count(); j > 0; j--) { + command = command.replace(QString("$%1").arg(j), params[j - 1]); + } + command = command.replace("$0", msg); + handleUserInput(bufferInfo, command); + } +} + + diff --git a/src/core/userinputhandler.h b/src/core/userinputhandler.h index 006e1ebe..81e5c178 100644 --- a/src/core/userinputhandler.h +++ b/src/core/userinputhandler.h @@ -64,6 +64,9 @@ public slots: void handleWhowas(const BufferInfo &bufferInfo, const QString &text); void defaultHandler(QString cmd, const BufferInfo &bufferInfo, const QString &text); + +private: + void expand(const QString &alias, const BufferInfo &bufferInfo, const QString &msg); }; diff --git a/src/qtui/mainwin.cpp b/src/qtui/mainwin.cpp index 9b46b73c..d426e4e7 100644 --- a/src/qtui/mainwin.cpp +++ b/src/qtui/mainwin.cpp @@ -50,6 +50,7 @@ #include "selectionmodelsynchronizer.h" #include "mappedselectionmodel.h" +#include "settingspages/aliasessettingspage.h" #include "settingspages/appearancesettingspage.h" #include "settingspages/bufferviewsettingspage.h" #include "settingspages/colorsettingspage.h" @@ -233,6 +234,7 @@ void MainWin::setupSettingsDlg() { //Category: Behaviour settingsDlg->registerSettingsPage(new GeneralSettingsPage(settingsDlg)); settingsDlg->registerSettingsPage(new HighlightSettingsPage(settingsDlg)); + settingsDlg->registerSettingsPage(new AliasesSettingsPage(settingsDlg)); //Category: General settingsDlg->registerSettingsPage(new IdentitiesSettingsPage(settingsDlg)); settingsDlg->registerSettingsPage(new NetworksSettingsPage(settingsDlg)); diff --git a/src/qtui/settingspages/aliasesmodel.cpp b/src/qtui/settingspages/aliasesmodel.cpp new file mode 100644 index 00000000..588673c2 --- /dev/null +++ b/src/qtui/settingspages/aliasesmodel.cpp @@ -0,0 +1,203 @@ +/*************************************************************************** + * Copyright (C) 2005-08 by the Quassel Project * + * devel@quassel-irc.org * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) version 3. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * 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. * + ***************************************************************************/ + +#include "aliasesmodel.h" + +#include +#include + +#include "client.h" +#include "signalproxy.h" + +AliasesModel::AliasesModel(QObject *parent) + : QAbstractItemModel(parent), + _configChanged(false) +{ +// _aliasManager.addAlias("a", "aa"); +// _aliasManager.addAlias("b", "bb"); +// _aliasManager.addAlias("c", "cc"); +// _aliasManager.addAlias("d", "dd"); + + // we need this signal for future connects to reset the data; + connect(Client::instance(), SIGNAL(connected()), this, SLOT(clientConnected())); + if(Client::isConnected()) + clientConnected(); +} + +QVariant AliasesModel::data(const QModelIndex &index, int role) const { + if(!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount()) + return QVariant(); + + switch(role) { + case Qt::ToolTipRole: + switch(index.column()) { + case 0: + return "The shortcut for the alias
" + "It can be used as a regular slash command.

" + "Example: \"foo\" can be used per /foo"; + case 1: + return "The string the shortcut will be expanded to
" + "$i represenents the i'th parameter. $0 the whole string.
" + "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"; + default: + return QVariant(); + } + case Qt::DisplayRole: + switch(index.column()) { + case 0: + return aliasManager()[index.row()].name; + case 1: + return aliasManager()[index.row()].expansion; + default: + return QVariant(); + } + default: + return QVariant(); + } +} + +bool AliasesModel::setData(const QModelIndex &index, const QVariant &value, int role) { + if(!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount() || role != Qt::EditRole) + return false; + + QString newValue = value.toString(); + if(newValue.isEmpty()) + return false; + + switch(index.column()) { + case 0: + if(aliasManager().contains(newValue)) { + return false; + } else { + cloneAliasManager()[index.row()].name = newValue; + return true; + } + 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::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; + } +} + + +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]; + + 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); +} + + +const AliasManager &AliasesModel::aliasManager() const { + if(_configChanged) + return _clonedAliasManager; + else + return _aliasManager; +} + +AliasManager &AliasesModel::aliasManager() { + if(_configChanged) + return _clonedAliasManager; + else + return _aliasManager; +} + +AliasManager &AliasesModel::cloneAliasManager() { + if(!_configChanged) { + _clonedAliasManager = _aliasManager; + _configChanged = true; + emit configChanged(true); + } + return _clonedAliasManager; +} + +void AliasesModel::revert() { + if(!_configChanged) + return; + + _configChanged = false; + emit configChanged(false); + reset(); +} + +void AliasesModel::commit() { + if(!_configChanged) + return; + + _aliasManager.requestUpdate(_clonedAliasManager.toVariantMap()); + revert(); +} + +void AliasesModel::initDone() { + reset(); + emit modelReady(); +} + +void AliasesModel::clientConnected() { + _aliasManager = AliasManager(); + Client::signalProxy()->synchronize(&_aliasManager); + connect(&_aliasManager, SIGNAL(initDone()), this, SLOT(initDone())); + connect(&_aliasManager, SIGNAL(updated(const QVariantMap &)), this, SLOT(revert())); +} diff --git a/src/qtui/settingspages/aliasesmodel.h b/src/qtui/settingspages/aliasesmodel.h new file mode 100644 index 00000000..f10c8186 --- /dev/null +++ b/src/qtui/settingspages/aliasesmodel.h @@ -0,0 +1,75 @@ +/*************************************************************************** + * Copyright (C) 2005-08 by the Quassel Project * + * devel@quassel-irc.org * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) version 3. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * 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. * + ***************************************************************************/ + +#ifndef ALIASESMODEL_H +#define ALIASESMODEL_H + +#include +#include + +#include "aliasmanager.h" + +class AliasesModel : public QAbstractItemModel { + Q_OBJECT + +public: + AliasesModel(QObject *parent = 0); + + virtual QVariant data(const QModelIndex &index, int role) const; + virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole); + + virtual Qt::ItemFlags flags(const QModelIndex &index) const; + + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + + QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; + + inline QModelIndex parent(const QModelIndex &) const { return QModelIndex(); } + + inline int rowCount(const QModelIndex &parent = QModelIndex()) const { Q_UNUSED(parent) return aliasManager().count(); } + inline int columnCount(const QModelIndex &parent = QModelIndex()) const { Q_UNUSED(parent) return 2; } + + inline bool configChanged() const { return _configChanged; } + +public slots: + void newAlias(); + void removeAlias(int index); + void revert(); + void commit(); + +signals: + void configChanged(bool); + void modelReady(); + +private: + AliasManager _aliasManager; + AliasManager _clonedAliasManager; + bool _configChanged; + + const AliasManager &aliasManager() const; + AliasManager &aliasManager(); + AliasManager &cloneAliasManager(); + +private slots: + void clientConnected(); + void initDone(); +}; + +#endif //ALIASESMODEL_H diff --git a/src/qtui/settingspages/aliasessettingspage.cpp b/src/qtui/settingspages/aliasessettingspage.cpp new file mode 100644 index 00000000..7aab01cc --- /dev/null +++ b/src/qtui/settingspages/aliasessettingspage.cpp @@ -0,0 +1,66 @@ +/*************************************************************************** + * Copyright (C) 2005-08 by the Quassel Project * + * devel@quassel-irc.org * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) version 3. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * 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. * + ***************************************************************************/ + +#include "aliasessettingspage.h" + +#include +#include + +AliasesSettingsPage::AliasesSettingsPage(QWidget *parent) + : SettingsPage(tr("Behaviour"), tr("Aliases"), parent) +{ + ui.setupUi(this); + + ui.aliasesView->setSelectionBehavior(QAbstractItemView::SelectRows); + ui.aliasesView->setSelectionMode(QAbstractItemView::SingleSelection); + ui.aliasesView->setAlternatingRowColors(true); + ui.aliasesView->setTabKeyNavigation(false); + ui.aliasesView->setModel(&_aliasesModel); + // ui.aliasesView->setSortingEnabled(true); + ui.aliasesView->verticalHeader()->hide(); + ui.aliasesView->horizontalHeader()->setStretchLastSection(true); + + connect(ui.newAliasButton, SIGNAL(clicked()), &_aliasesModel, SLOT(newAlias())); + connect(ui.deleteAliasButton, SIGNAL(clicked()), this, SLOT(deleteSelectedAlias())); + connect(&_aliasesModel, SIGNAL(configChanged(bool)), this, SLOT(setChangedState(bool))); + connect(&_aliasesModel, SIGNAL(modelReady()), this, SLOT(enableDialog())); +} + +void AliasesSettingsPage::load() { + if(_aliasesModel.configChanged()) + _aliasesModel.revert(); +} + +void AliasesSettingsPage::save() { + if(_aliasesModel.configChanged()) + _aliasesModel.commit(); +} + +void AliasesSettingsPage::enableDialog() { + ui.newAliasButton->setEnabled(true); + ui.deleteAliasButton->setEnabled(true); +} + +void AliasesSettingsPage::deleteSelectedAlias() { + if(!ui.aliasesView->selectionModel()->hasSelection()) + return; + + _aliasesModel.removeAlias(ui.aliasesView->selectionModel()->selectedIndexes()[0].row()); +} diff --git a/src/qtui/settingspages/aliasessettingspage.h b/src/qtui/settingspages/aliasessettingspage.h new file mode 100644 index 00000000..de1888c8 --- /dev/null +++ b/src/qtui/settingspages/aliasessettingspage.h @@ -0,0 +1,55 @@ +/*************************************************************************** + * Copyright (C) 2005-08 by the Quassel Project * + * devel@quassel-irc.org * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) version 3. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * 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. * + ***************************************************************************/ + +#ifndef ALIASESSETTINGSPAGE_H +#define ALIASESSETTINGSPAGE_H + +#include "settingspage.h" +#include "ui_aliasessettingspage.h" + +#include "aliasesmodel.h" + +class AliasesSettingsPage : public SettingsPage { + Q_OBJECT + +public: + AliasesSettingsPage(QWidget *parent = 0); + +public slots: + void save(); + void load(); +// void defaults(); + +// private slots: +// void widgetHasChanged(); + +private: + Ui::AliasesSettingsPage ui; + + AliasesModel _aliasesModel; + + // bool testHasChanged(); + +private slots: + void enableDialog(); + void deleteSelectedAlias(); +}; + +#endif //ALIASESSETTINGSPAGE_H diff --git a/src/qtui/settingspages/aliasessettingspage.ui b/src/qtui/settingspages/aliasessettingspage.ui new file mode 100644 index 00000000..4948c4f6 --- /dev/null +++ b/src/qtui/settingspages/aliasessettingspage.ui @@ -0,0 +1,70 @@ + + AliasesSettingsPage + + + + 0 + 0 + 291 + 229 + + + + Form + + + + + + + + + + + false + + + New + + + + :/16x16/actions/oxygen/16x16/actions/list-add.png:/16x16/actions/oxygen/16x16/actions/list-add.png + + + + + + + false + + + Delete + + + + :/16x16/actions/oxygen/16x16/actions/edit-delete.png:/16x16/actions/oxygen/16x16/actions/edit-delete.png + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + + diff --git a/src/qtui/settingspages/settingspages.inc b/src/qtui/settingspages/settingspages.inc index b3d93b66..db6a2403 100644 --- a/src/qtui/settingspages/settingspages.inc +++ b/src/qtui/settingspages/settingspages.inc @@ -1,8 +1,8 @@ # Putting $FOO in SETTINGSPAGES automatically includes # $FOOsettingspage.cpp, $FOOsettingspage.h and $FOOsettingspage.ui -set(SETTINGSPAGES appearance bufferview color fonts general highlight identities networks) +set(SETTINGSPAGES aliases appearance bufferview color fonts general highlight identities networks) # Specify additional files (e.g. for subdialogs) here! -set(SP_SOURCES ) -set(SP_HEADERS ) +set(SP_SOURCES aliasesmodel.cpp) +set(SP_HEADERS aliasesmodel.h) set(SP_FORMS buffervieweditdlg.ui createidentitydlg.ui saveidentitiesdlg.ui networkeditdlg.ui nickeditdlg.ui servereditdlg.ui) diff --git a/src/uisupport/settingspage.cpp b/src/uisupport/settingspage.cpp index 7808a740..e752ec72 100644 --- a/src/uisupport/settingspage.cpp +++ b/src/uisupport/settingspage.cpp @@ -20,38 +20,12 @@ #include "settingspage.h" -SettingsPage::SettingsPage(const QString &category, const QString &title, QWidget *parent) : QWidget(parent), - _category(category), _title(title) { - - _changed = false; -} - -QString SettingsPage::category() const { - return _category; -} - -QString SettingsPage::title() const { - return _title; -} - -bool SettingsPage::hasDefaults() const { - return false; -} - -void SettingsPage::defaults() { - -} - -bool SettingsPage::hasChanged() const { - return _changed; -} - -bool SettingsPage::aboutToSave() { - return true; -} - -void SettingsPage::changed() { - setChangedState(true); +SettingsPage::SettingsPage(const QString &category, const QString &title, QWidget *parent) + : QWidget(parent), + _category(category), + _title(title), + _changed(false) +{ } void SettingsPage::setChangedState(bool hasChanged) { diff --git a/src/uisupport/settingspage.h b/src/uisupport/settingspage.h index f58cb377..a9f32b8f 100644 --- a/src/uisupport/settingspage.h +++ b/src/uisupport/settingspage.h @@ -31,60 +31,59 @@ class SettingsPage : public QWidget { Q_OBJECT - public: - SettingsPage(const QString &category, const QString &name, QWidget *parent = 0); - virtual ~SettingsPage() {}; - - //! The category of this settings page. - virtual QString category() const; - - //! The title of this settings page. - virtual QString title() const; - - //! Derived classes need to define this and return true if they have default settings. - /** If this method returns true, the "Restore Defaults" button in the SettingsDlg is - * enabled. You also need to provide an implementation of defaults() then. - * - * The default implementation returns false. - */ - virtual bool hasDefaults() const; - - //! Check if there are changes in the page, compared to the state saved in permanent storage. - bool hasChanged() const; - - //! Called immediately before save() is called. - /** Derived classes should return false if saving is not possible (e.g. the current settings are invalid). - * \return false, if the SettingsPage cannot be saved in its current state. - */ - virtual bool aboutToSave(); - - public slots: - //! Save settings to permanent storage. - virtual void save() = 0; - - //! Load settings from permanent storage, overriding any changes the user might have made in the dialog. - virtual void load() = 0; - - //! Restore defaults, overriding any changes the user might have made in the dialog. - /** The default implementation does nothing. +public: + SettingsPage(const QString &category, const QString &name, QWidget *parent = 0); + virtual ~SettingsPage() {}; + + //! The category of this settings page. + inline virtual QString category() const { return _category; } + + //! The title of this settings page. + inline virtual QString title() const { return _title; } + + //! Derived classes need to define this and return true if they have default settings. + /** If this method returns true, the "Restore Defaults" button in the SettingsDlg is + * enabled. You also need to provide an implementation of defaults() then. + * + * The default implementation returns false. */ - virtual void defaults(); - - protected slots: - //! Calling this slot is equivalent to calling setChangedState(true). - void changed(); - - protected: - //! This should be called whenever the widget state changes from unchanged to change or the other way round. - void setChangedState(bool hasChanged = true); - - signals: - //! Emitted whenever the widget state changes. - void changed(bool hasChanged); - - private: - QString _category, _title; - bool _changed; + inline virtual bool hasDefaults() const { return false; } + + //! Check if there are changes in the page, compared to the state saved in permanent storage. + inline bool hasChanged() const { return _changed; } + + //! Called immediately before save() is called. + /** Derived classes should return false if saving is not possible (e.g. the current settings are invalid). + * \return false, if the SettingsPage cannot be saved in its current state. + */ + inline virtual bool aboutToSave() { return true; } + +public slots: + //! Save settings to permanent storage. + virtual void save() = 0; + + //! Load settings from permanent storage, overriding any changes the user might have made in the dialog. + virtual void load() = 0; + + //! Restore defaults, overriding any changes the user might have made in the dialog. + /** The default implementation does nothing. + */ + inline virtual void defaults() {} + +protected slots: + //! Calling this slot is equivalent to calling setChangedState(true). + inline void changed() { setChangedState(true); } + + //! This should be called whenever the widget state changes from unchanged to change or the other way round. + void setChangedState(bool hasChanged = true); + +signals: + //! Emitted whenever the widget state changes. + void changed(bool hasChanged); + +private: + QString _category, _title; + bool _changed; }; #endif -- 2.20.1