[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: [ktp-common-internals] /: Add a small application to show contact-list-model
From: David Edmundson <kde () davidedmundson ! co ! uk>
Date: 2013-02-28 22:31:31
Message-ID: 20130228223131.E67D2A604F () git ! kde ! org
[Download RAW message or body]
Git commit 9c002a1cf63bd5c56e3f49e074db6026d06fca1b by David Edmundson.
Committed on 25/02/2013 at 02:03.
Pushed by davidedmundson into branch 'master'.
Add a small application to show contact-list-model
REVIEW: 109132
M +2 -0 CMakeLists.txt
A +31 -0 tests/CMakeLists.txt
A +99 -0 tests/contact-list-model-view-main.cpp [License: LGPL (v2.1+)]
A +43 -0 tests/model-view.cpp [License: LGPL (v2.1+)]
A +42 -0 tests/model-view.h [License: LGPL (v2.1+)]
A +24 -0 tests/model-view.ui
A +168 -0 tests/roles-proxy-model.cpp [License: LGPL (v2.1+)]
A +59 -0 tests/roles-proxy-model.h [License: LGPL (v2.1+)]
http://commits.kde.org/telepathy-common-internals/9c002a1cf63bd5c56e3f49e074db6026d06fca1b
diff --git a/CMakeLists.txt b/CMakeLists.txt
index de94874..6b7cf7b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -40,3 +40,5 @@ configure_file(version.h.in ${CMAKE_CURRENT_BINARY_DIR}/version.h \
@ONLY) add_subdirectory(KTp)
add_subdirectory(tools)
add_subdirectory(data)
+add_subdirectory(tests)
+
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
new file mode 100644
index 0000000..4694b75
--- /dev/null
+++ b/tests/CMakeLists.txt
@@ -0,0 +1,31 @@
+set( EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR} )
+
+include_directories(
+ ${CMAKE_SOURCE_DIR}
+ ${CMAKE_CURRENT_BINARY_DIR}/..
+ ${CMAKE_CURRENT_SOURCE_DIR}/..
+ )
+
+
+
+set(ktp_contact_list_model_view_SRCS
+ contact-list-model-view-main.cpp
+ model-view.cpp
+ roles-proxy-model.cpp
+)
+
+kde4_add_ui_files(ktp_contact_list_model_view_SRCS model-view.ui)
+
+kde4_add_executable(ktp_contact_list_model_view
+ ${ktp_contact_list_model_view_SRCS}
+)
+
+target_link_libraries(ktp_contact_list_model_view
+ ${QT_QTTEST_LIBRARY}
+ ${KDE4_KDECORE_LIBS}
+ ${KDE4_KDEUI_LIBS}
+ ${TELEPATHY_QT4_LIBRARIES}
+ ktpcommoninternalsprivate
+ ktpmodelsprivate
+)
+
diff --git a/tests/contact-list-model-view-main.cpp \
b/tests/contact-list-model-view-main.cpp new file mode 100644
index 0000000..ca7cab1
--- /dev/null
+++ b/tests/contact-list-model-view-main.cpp
@@ -0,0 +1,99 @@
+/*
+ * This file is part of telepathy-kde-models-test-ui
+ *
+ * Copyright (C) 2011 Collabora Ltd. <info@collabora.co.uk>
+ * @Author George Goldberg <george.goldberg@collabora.co.uk>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "model-view.h"
+
+#include <KAboutData>
+#include <KCmdLineArgs>
+#include <KDebug>
+#include <KApplication>
+
+#include <TelepathyQt/Types>
+#include <TelepathyQt/Debug>
+
+#include <TelepathyQt/AccountManager>
+#include <TelepathyQt/AccountFactory>
+#include <TelepathyQt/ConnectionFactory>
+#include <TelepathyQt/TextChannel>
+
+#include <QDBusConnection>
+
+#include "KTp/Models/contacts-list-model.h"
+#include "KTp/contact-factory.h"
+
+int main(int argc, char *argv[])
+{
+ KAboutData aboutData("telepathy-kde-models-test-ui",
+ 0,
+ ki18n("Telepathy KDE Models Test UI"),
+ "0.1",
+ ki18n("Telepathy KDE Models Test UI"),
+ KAboutData::License_LGPL,
+ ki18n("(C) 2011 Collabora Ltd"));
+
+ KCmdLineArgs::init(argc, argv, &aboutData);
+
+ KApplication app;
+
+ Tp::registerTypes();
+ Tp::enableDebug(false);
+ Tp::enableWarnings(true);
+
+
+ Tp::AccountFactoryPtr accountFactory = \
Tp::AccountFactory::create(QDBusConnection::sessionBus(), + \
Tp::Features() << Tp::Account::FeatureCore + \
<< Tp::Account::FeatureAvatar + \
<< Tp::Account::FeatureCapabilities + \
<< Tp::Account::FeatureProtocolInfo + \
<< Tp::Account::FeatureProfile); +
+ Tp::ConnectionFactoryPtr connectionFactory = \
Tp::ConnectionFactory::create(QDBusConnection::sessionBus(), + \
Tp::Features() << Tp::Connection::FeatureCore + \
<< Tp::Connection::FeatureRosterGroups + \
<< Tp::Connection::FeatureRoster + \
<< Tp::Connection::FeatureSelfContact); +
+ Tp::ContactFactoryPtr contactFactory = \
KTp::ContactFactory::create(Tp::Features() << Tp::Contact::FeatureAlias + \
<< Tp::Contact::FeatureAvatarData + \
<< Tp::Contact::FeatureSimplePresence + \
<< Tp::Contact::FeatureCapabilities + \
<< Tp::Contact::FeatureClientTypes); +
+ Tp::ChannelFactoryPtr channelFactory = \
Tp::ChannelFactory::create(QDBusConnection::sessionBus()); + \
channelFactory->addFeaturesForTextChats(Tp::Features() << Tp::Channel::FeatureCore << \
Tp::TextChannel::FeatureMessageQueue); +
+ Tp::AccountManagerPtr accountManager = \
Tp::AccountManager::create(QDBusConnection::sessionBus(), + \
accountFactory, + connectionFactory,
+ channelFactory,
+ contactFactory);
+
+ KTp::ContactsListModel *model = new KTp::ContactsListModel(&app);
+ model->setAccountManager(accountManager);
+
+ // Set up and show the main widget
+ ModelView *mainWidget = new ModelView(model, 0);
+ mainWidget->show();
+
+ // Start event loop.
+ app.exec();
+}
+
diff --git a/tests/model-view.cpp b/tests/model-view.cpp
new file mode 100644
index 0000000..25ed46b
--- /dev/null
+++ b/tests/model-view.cpp
@@ -0,0 +1,43 @@
+/*
+ * This file is part of telepathy-kde-models-test-ui
+ *
+ * Copyright (C) 2011 Collabora Ltd. <info@collabora.co.uk>
+ * Copyright (C) 2013 David Edmundson <davidedmundson@kde.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "model-view.h"
+
+#include "roles-proxy-model.h"
+
+
+
+ModelView::ModelView(QAbstractItemModel *model, QWidget *parent)
+ : QWidget(parent)
+{
+ setupUi(this);
+
+ RolesProxyModel *proxyModel = new RolesProxyModel(this);
+ proxyModel->setSourceModel(model);
+
+ TreeView->setModel(proxyModel);
+}
+
+ModelView::~ModelView()
+{
+
+}
+
diff --git a/tests/model-view.h b/tests/model-view.h
new file mode 100644
index 0000000..f0ad9f7
--- /dev/null
+++ b/tests/model-view.h
@@ -0,0 +1,42 @@
+/*
+ * This file is part of telepathy-kde-models-test-ui
+ *
+ * Copyright (C) 2011 Collabora Ltd. <info@collabora.co.uk>
+ * Copyright (C) 2013 David Edmundson <davidedmundson@kde.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef MAIN_WIDGET_H
+#define MAIN_WIDGET_H
+
+#include "ui_model-view.h"
+
+#include <QWidget>
+#include <QAbstractItemModel>
+
+class ModelView : public QWidget, protected Ui::MainWidget {
+
+ Q_OBJECT
+
+public:
+ ModelView(QAbstractItemModel *model, QWidget *parent = 0);
+ ~ModelView();
+
+};
+
+
+#endif
+
diff --git a/tests/model-view.ui b/tests/model-view.ui
new file mode 100644
index 0000000..75450e1
--- /dev/null
+++ b/tests/model-view.ui
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>MainWidget</class>
+ <widget class="QWidget" name="MainWidget">
+ <property name="geometry">
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>400</width>
+ <height>300</height>
+ </rect>
+ </property>
+ <property name="windowTitle">
+ <string>Telepathy-KDE Models Testing UI</string>
+ </property>
+ <layout class="QHBoxLayout" name="horizontalLayout">
+ <item>
+ <widget class="QTreeView" name="TreeView"/>
+ </item>
+ </layout>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>
diff --git a/tests/roles-proxy-model.cpp b/tests/roles-proxy-model.cpp
new file mode 100644
index 0000000..6a0c3e1
--- /dev/null
+++ b/tests/roles-proxy-model.cpp
@@ -0,0 +1,168 @@
+/*
+ * This file is part of telepathy-kde-models-test-ui
+ *
+ * Copyright (C) 2011 Collabora Ltd. <info@collabora.co.uk>
+ * Copyright (C) 2013 David Edmundson <davidedmundson@kde.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "roles-proxy-model.h"
+
+#include <KDebug>
+
+RolesProxyModel::RolesProxyModel(QObject *parent)
+ : QAbstractProxyModel(parent)
+{
+ kDebug();
+}
+
+RolesProxyModel::~RolesProxyModel()
+{
+ kDebug();
+}
+
+void RolesProxyModel::setSourceModel(QAbstractItemModel *sourceModel)
+{
+ connect(sourceModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
+ this, SLOT(onSourceRowsInserted(QModelIndex,int,int)));
+ connect(sourceModel, SIGNAL(rowsRemoved(QModelIndex,int,int)),
+ this, SLOT(onSourceRowsRemoved(QModelIndex,int,int)));
+ connect(sourceModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
+ this, SLOT(onSourceDataChanged(QModelIndex,QModelIndex)));
+ connect(sourceModel, SIGNAL(layoutChanged()),
+ this, SLOT(onSourceLayoutChanged()));
+
+ QAbstractProxyModel::setSourceModel(sourceModel);
+}
+
+int RolesProxyModel::columnCount(const QModelIndex &parent) const
+{
+ // Number of columns is number of roles available to QML
+ return sourceModel()->roleNames().size();
+}
+
+int RolesProxyModel::rowCount(const QModelIndex &parent) const
+{
+ return sourceModel()->rowCount(mapToSource(parent));
+}
+
+QModelIndex RolesProxyModel::index(int row, int column, const QModelIndex &parent) \
const +{
+ // this is a list, not a tree, so parent must be invalid (root item)
+ if (parent.isValid()) {
+ return QModelIndex();
+ }
+
+ if (column >= columnCount() || column < 0) {
+ return QModelIndex();
+ }
+
+ // Check the row is within the bounds of the list
+ if (row >= rowCount() || row < 0) {
+ return QModelIndex();
+ }
+
+ // Return the index to the item.
+ return createIndex(row, column, 0);
+}
+
+QModelIndex RolesProxyModel::parent(const QModelIndex &index) const
+{
+ Q_UNUSED(index);
+
+ // Not a tree model, so all items have root-item as parent.
+ return QModelIndex();
+}
+
+QVariant RolesProxyModel::data(const QModelIndex &index, int role) const
+{
+ if (!index.isValid()) {
+ return QVariant();
+ }
+
+ if (index.column() >= columnCount() || index.column() < 0) {
+ return QVariant();
+ }
+
+ if (index.row() >= rowCount() || index.row() < 0) {
+ return QVariant();
+ }
+
+ if (role == Qt::DisplayRole) {
+ return sourceModel()->data(mapToSource(index), \
sourceModel()->roleNames().keys().at(index.column())); + }
+
+ return sourceModel()->data(mapToSource(index), role);
+}
+
+Qt::ItemFlags RolesProxyModel::flags(const QModelIndex &index) const
+{
+ // Pass through from source model
+ return sourceModel()->flags(mapToSource(index));
+}
+
+QVariant RolesProxyModel::headerData(int section, Qt::Orientation orientation, int \
role) const +{
+ if (role == Qt::DisplayRole) {
+ return sourceModel()->roleNames().values().at(section);
+ }
+
+ return QVariant();
+}
+
+QModelIndex RolesProxyModel::mapFromSource(const QModelIndex &sourceIndex) const
+{
+ if (!sourceIndex.isValid()) {
+ return QModelIndex();
+ }
+
+ return index(sourceIndex.row(), sourceIndex.column(), \
mapFromSource(sourceIndex.parent())); +}
+
+QModelIndex RolesProxyModel::mapToSource(const QModelIndex &proxyIndex) const
+{
+ if (!proxyIndex.isValid()) {
+ return QModelIndex();
+ }
+
+ return sourceModel()->index(proxyIndex.row(), 0, \
mapToSource(proxyIndex.parent())); +}
+
+void RolesProxyModel::onSourceRowsInserted(const QModelIndex &parent, int start, int \
end) +{
+ beginInsertRows(mapFromSource(parent), start, end);
+ endInsertRows();
+}
+
+void RolesProxyModel::onSourceRowsRemoved(const QModelIndex &parent, int start, int \
end) +{
+ beginRemoveRows(mapFromSource(parent), start, end);
+ endRemoveRows();
+}
+
+void RolesProxyModel::onSourceDataChanged(const QModelIndex &topLeft, const \
QModelIndex &bottomRight) +{
+ // Assume that since the data is layed out in the same way in the proxy model \
that we can just + // translate the ranges to relay the signal
+ dataChanged(mapFromSource(topLeft), mapFromSource(bottomRight));
+}
+
+void RolesProxyModel::onSourceLayoutChanged()
+{
+ layoutAboutToBeChanged();
+ layoutChanged();
+}
+
diff --git a/tests/roles-proxy-model.h b/tests/roles-proxy-model.h
new file mode 100644
index 0000000..4766110
--- /dev/null
+++ b/tests/roles-proxy-model.h
@@ -0,0 +1,59 @@
+/*
+ * This file is part of telepathy-kde-models-test-ui
+ *
+ * Copyright (C) 2011 Collabora Ltd. <info@collabora.co.uk>
+ * Copyright (C) 2013 David Edmundson <davidedmundson@kde.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef ROLES_PROXY_MODEL_H
+#define ROLES_PROXY_MODEL_H
+
+#include <QAbstractProxyModel>
+
+class RolesProxyModel : public QAbstractProxyModel {
+
+ Q_OBJECT
+
+public:
+ RolesProxyModel(QObject *parent = 0);
+ virtual ~RolesProxyModel();
+
+ virtual void setSourceModel(QAbstractItemModel *sourceModel);
+
+ virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
+ virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
+ virtual QModelIndex index(int row, int column, const QModelIndex &parent = \
QModelIndex()) const; + virtual QModelIndex parent(const QModelIndex &index) \
const; +
+ virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) \
const; + virtual Qt::ItemFlags flags(const QModelIndex &index) const;
+ virtual QVariant headerData(int section, Qt::Orientation orientation, int role = \
Qt::DisplayRole) const; +
+ virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const;
+ virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const;
+
+protected Q_SLOTS:
+ virtual void onSourceRowsInserted(const QModelIndex &parent, int start, int \
end); + virtual void onSourceRowsRemoved(const QModelIndex &parent, int start, int \
end); + virtual void onSourceDataChanged(const QModelIndex &topLeft, const \
QModelIndex &bottomRight); + virtual void onSourceLayoutChanged();
+
+};
+
+
+#endif
+
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic