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 (v= 2.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/9c002a1cf63bd5c56e3f49e07= 4db6026d06fca1b 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}/v= ersion.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-mo= del-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. + * @Author George Goldberg + * + * 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 +#include +#include +#include + +#include +#include + +#include +#include +#include +#include + +#include + +#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 =3D Tp::AccountFactory::crea= te(QDBusConnection::sessionBus(), + Tp:= :Features() << Tp::Account::FeatureCore + << = Tp::Account::FeatureAvatar + << = Tp::Account::FeatureCapabilities + << = Tp::Account::FeatureProtocolInfo + << = Tp::Account::FeatureProfile); + + Tp::ConnectionFactoryPtr connectionFactory =3D Tp::ConnectionFactory::= create(QDBusConnection::sessionBus(), + = Tp::Features() << Tp::Connection::FeatureCore + = << Tp::Connection::FeatureRosterGroups + = << Tp::Connection::FeatureRoster + = << Tp::Connection::FeatureSelfContact); + + Tp::ContactFactoryPtr contactFactory =3D KTp::ContactFactory::create(T= p::Features() << Tp::Contact::FeatureAlias + << T= p::Contact::FeatureAvatarData + << T= p::Contact::FeatureSimplePresence + << T= p::Contact::FeatureCapabilities + << T= p::Contact::FeatureClientTypes); + + Tp::ChannelFactoryPtr channelFactory =3D Tp::ChannelFactory::create(QD= BusConnection::sessionBus()); + channelFactory->addFeaturesForTextChats(Tp::Features() << Tp::Channel:= :FeatureCore << Tp::TextChannel::FeatureMessageQueue); + + Tp::AccountManagerPtr accountManager =3D Tp::AccountManager::create(QD= BusConnection::sessionBus(), + accountFactory, + connectionFactory, + channelFactory, + contactFactory); + + KTp::ContactsListModel *model =3D new KTp::ContactsListModel(&app); + model->setAccountManager(accountManager); + = + // Set up and show the main widget + ModelView *mainWidget =3D 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. + * Copyright (C) 2013 David Edmundson + * + * 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 =3D 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. + * Copyright (C) 2013 David Edmundson + * + * 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 +#include + +class ModelView : public QWidget, protected Ui::MainWidget { + + Q_OBJECT + +public: + ModelView(QAbstractItemModel *model, QWidget *parent =3D 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 @@ + + + MainWidget + + + + 0 + 0 + 400 + 300 + + + + Telepathy-KDE Models Testing 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. + * Copyright (C) 2013 David Edmundson + * + * 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 + +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 >=3D columnCount() || column < 0) { + return QModelIndex(); + } + + // Check the row is within the bounds of the list + if (row >=3D 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() >=3D columnCount() || index.column() < 0) { + return QVariant(); + } + + if (index.row() >=3D rowCount() || index.row() < 0) { + return QVariant(); + } + + if (role =3D=3D Qt::DisplayRole) { + return sourceModel()->data(mapToSource(index), sourceModel()->role= Names().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 orientat= ion, int role) const +{ + if (role =3D=3D 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(so= urceIndex.parent())); +} + +QModelIndex RolesProxyModel::mapToSource(const QModelIndex &proxyIndex) co= nst +{ + if (!proxyIndex.isValid()) { + return QModelIndex(); + } + + return sourceModel()->index(proxyIndex.row(), 0, mapToSource(proxyInde= x.parent())); +} + +void RolesProxyModel::onSourceRowsInserted(const QModelIndex &parent, int = start, int end) +{ + beginInsertRows(mapFromSource(parent), start, end); + endInsertRows(); +} + +void RolesProxyModel::onSourceRowsRemoved(const QModelIndex &parent, int s= tart, int end) +{ + beginRemoveRows(mapFromSource(parent), start, end); + endRemoveRows(); +} + +void RolesProxyModel::onSourceDataChanged(const QModelIndex &topLeft, cons= t QModelIndex &bottomRight) +{ + // Assume that since the data is layed out in the same way in the prox= y 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. + * Copyright (C) 2013 David Edmundson + * + * 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 + +class RolesProxyModel : public QAbstractProxyModel { + + Q_OBJECT + +public: + RolesProxyModel(QObject *parent =3D 0); + virtual ~RolesProxyModel(); + + virtual void setSourceModel(QAbstractItemModel *sourceModel); + + virtual int rowCount(const QModelIndex &parent =3D QModelIndex()) cons= t; + virtual int columnCount(const QModelIndex &parent =3D QModelIndex()) c= onst; + virtual QModelIndex index(int row, int column, const QModelIndex &pare= nt =3D QModelIndex()) const; + virtual QModelIndex parent(const QModelIndex &index) const; + + virtual QVariant data(const QModelIndex &index, int role =3D Qt::Displ= ayRole) const; + virtual Qt::ItemFlags flags(const QModelIndex &index) const; + virtual QVariant headerData(int section, Qt::Orientation orientation, = int role =3D Qt::DisplayRole) const; + + virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) cons= t; + 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 QMo= delIndex &bottomRight); + virtual void onSourceLayoutChanged(); + +}; + + +#endif +