[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: =?utf-8?q?=5Baki=5D_aki=3A_Added_ServerModel_class?=
From: Keith Rusler <xzekecomax () gmail ! com>
Date: 2011-02-06 1:45:31
Message-ID: 20110206014531.D4BF7A609B () git ! kde ! org
[Download RAW message or body]
Git commit 2b7720fc85c687adadbf9591c984b0b95c0108e3 by Keith Rusler.
Committed on 06/02/11 at 02:44.
Pushed by karusler into branch 'master'.
Added ServerModel class
M +1 -0 aki/ui/CMakeLists.txt
A +133 -0 aki/ui/servermodel.cxx [License: GPL (v2/3)]
A +115 -0 aki/ui/servermodel.hpp [License: GPL (v2/3)]
http://commits.kde.org/aki/2b7720fc85c687adadbf9591c984b0b95c0108e3
diff --git a/aki/ui/CMakeLists.txt b/aki/ui/CMakeLists.txt
index 8311b4c..784c86d 100644
--- a/aki/ui/CMakeLists.txt
+++ b/aki/ui/CMakeLists.txt
@@ -12,6 +12,7 @@ set(aki_ui_SRCS
ui/networkmodel.cxx
ui/nicknamelist.cxx
ui/nicknamemodel.cxx
+ ui/servermodel.cxx
ui/systemtray.cxx
ui/userlist.cxx
ui/userlistdelegate.cxx
diff --git a/aki/ui/servermodel.cxx b/aki/ui/servermodel.cxx
new file mode 100644
index 0000000..55bd518
--- /dev/null
+++ b/aki/ui/servermodel.cxx
@@ -0,0 +1,133 @@
+/*
+ * Copyright 2009-2011 Keith Rusler <xzekecomax@gmail.com>
+ *
+ * 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 or any later version
+ * accepted by the membership of KDE e.V. (or its successor approved
+ * by the membership of KDE e.V.), which shall act as a proxy
+ * defined in Section 14 of version 3 of the license.
+ *
+ * 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, see <http://www.gnu.org/licenses/>
+ */
+
+#include "servermodel.hpp"
+#include "sql/server.hpp"
+using namespace Aki;
+
+ServerModel::ServerModel(QObject* parent)
+ : QAbstractListModel(parent)
+{
+}
+
+ServerModel::~ServerModel()
+{
+ qDeleteAll(_serverList);
+}
+
+void
+ServerModel::addServer(Aki::Sql::Server* server)
+{
+ beginInsertRows(QModelIndex(), rowCount(), rowCount());
+ _serverList.append(server);
+ endInsertRows();
+}
+
+QVariant
+ServerModel::data(const QModelIndex& index, int role) const
+{
+ if (!index.isValid()) {
+ return QVariant();
+ }
+
+ Aki::Sql::Server* server = _serverList.at(index.row());
+ if (!server) {
+ return QVariant();
+ }
+
+ switch (role) {
+ case Qt::DisplayRole: {
+ return server->name();
+ break;
+ }
+ default: {
+ return QVariant();
+ }
+ }
+
+ return QVariant();
+}
+
+Qt::ItemFlags
+ServerModel::flags(const QModelIndex& index) const
+{
+ if (!index.isValid()) {
+ return Qt::NoItemFlags;
+ }
+
+ return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
+}
+
+void
+ServerModel::insertServer(int row, Aki::Sql::Server* server)
+{
+ if (!server) {
+ return;
+ }
+
+ if (row >= rowCount()) {
+ addServer(server);
+ return;
+ } else {
+ beginInsertRows(QModelIndex(), 0, 0);
+ _serverList.prepend(server);
+ endInsertRows();
+ return;
+ }
+
+ beginInsertRows(QModelIndex(), row, row);
+ _serverList.insert(row, server);
+ endInsertRows();
+}
+
+void
+ServerModel::removeServer(Aki::Sql::Server* server)
+{
+ if (!server) {
+ return;
+ }
+
+ const int row = _serverList.indexOf(server);
+ beginRemoveRows(QModelIndex(), row, row);
+ delete takeServer(row);
+ endRemoveRows();
+}
+
+int
+ServerModel::rowCount(const QModelIndex&) const
+{
+ return _serverList.count();
+}
+
+const QList<Aki::Sql::Server*>&
+ServerModel::servers() const
+{
+ return _serverList;
+}
+
+Aki::Sql::Server*
+ServerModel::takeServer(int row)
+{
+ beginRemoveRows(QModelIndex(), row, row);
+ Aki::Sql::Server* server = _serverList.takeAt(row);
+ endRemoveRows();
+
+ return server;
+}
diff --git a/aki/ui/servermodel.hpp b/aki/ui/servermodel.hpp
new file mode 100644
index 0000000..d4d792c
--- /dev/null
+++ b/aki/ui/servermodel.hpp
@@ -0,0 +1,115 @@
+/*
+ * Copyright 2009-2011 Keith Rusler <xzekecomax@gmail.com>
+ *
+ * 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 or any later version
+ * accepted by the membership of KDE e.V. (or its successor approved
+ * by the membership of KDE e.V.), which shall act as a proxy
+ * defined in Section 14 of version 3 of the license.
+ *
+ * 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, see <http://www.gnu.org/licenses/>
+ */
+
+#ifndef AKI_SERVERMODEL_HPP
+#define AKI_SERVERMODEL_HPP
+
+#include <QtCore/QAbstractItemModel>
+
+namespace Aki
+{
+namespace Sql
+{
+class Server;
+} // End of namespace Sql.
+class ServerModel
+ : public QAbstractListModel
+{
+ Q_OBJECT
+public:
+ /**
+ * Creates a new ServerModel with a @p parent.
+ *
+ * @param parent Parent of the object.
+ */
+ explicit ServerModel(QObject* parent = 0);
+ /**
+ * Destroys the object.
+ */
+ ~ServerModel();
+ /**
+ * Adds an @p server to the end of the list.
+ *
+ * @param server Server.
+ */
+ void addServer(Aki::Sql::Server* server);
+ /**
+ * Gets the data stored under the given @p role for the item referred to by the \
@p index. + * @note If you do not have a value to return, return an invalid \
QVariant instead of returning 0. + *
+ * @param index Index of the item to get the value of.
+ * @param role Model role to get the value of the item at the @p index.
+ *
+ * @return Data for the given @p role of the item at @p index. QVariant() if no \
value is to be returned. + */
+ virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) \
const; + /**
+ * Gets the item flags for the given @p index.
+ *
+ * @param index Item to receive the flags for.
+ *
+ * @return Flags for the item at the given @p index.
+ */
+ virtual Qt::ItemFlags flags(const QModelIndex& index) const;
+ /**
+ * Inserts the @p server at the given @p row.
+ * @note If the @p row is below 0 it will be pushed at the beginning.
+ * @note If the @p row is more than the number of items in the model it will be \
pushed at the end. + *
+ * @param row Row to insert the @p server at.
+ * @param server Server.
+ */
+ void insertServer(int row, Aki::Sql::Server* server);
+ /**
+ * Removes the @p server from the model and deletes it. The @p server will be \
invalid after it returns. + *
+ * @param server Server.
+ */
+ void removeServer(Aki::Sql::Server* server);
+ /**
+ * Gets the number of items in the model.
+ *
+ * @param parent Parent is ignored.
+ *
+ * @return Number of items in the model.
+ */
+ virtual int rowCount(const QModelIndex& parent = QModelIndex()) const;
+ /**
+ * Gets a list of servers this model currently contains.
+ *
+ * @return List of servers in this model; empty list if no servers are found.
+ */
+ const QList<Aki::Sql::Server*>& servers() const;
+ /**
+ * Gets the item at the given @p row. This will remove the item from the model
+ * but it will not delete it. You are responsible for deletion of the item if
+ * the return value is not 0.
+ *
+ * @param row Row index.
+ *
+ * @return Server at the given @p row; 0 if the @p row is invalid.
+ */
+ Aki::Sql::Server* takeServer(int row);
+private:
+ QList<Aki::Sql::Server*> _serverList;
+}; // End of class ServerModel.
+} // End of namespace Aki.
+
+#endif // AKI_SERVERMODEL_HPP
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic