File indexing completed on 2024-04-28 15:29:25

0001 /*
0002     Persons Model Example
0003     SPDX-FileCopyrightText: 2012 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-or-later
0006 */
0007 
0008 #include <QApplication>
0009 #include <QSortFilterProxyModel>
0010 #include <QTreeView>
0011 
0012 #include <QPushButton>
0013 #include <QVBoxLayout>
0014 
0015 #include <QPainter>
0016 #include <QStyledItemDelegate>
0017 
0018 #include <personsmodel.h>
0019 
0020 using namespace KPeople;
0021 
0022 const int SPACING = 8;
0023 const int PHOTO_SIZE = 32;
0024 
0025 class PersonsDelegate : public QStyledItemDelegate
0026 {
0027 public:
0028     PersonsDelegate(QObject *parent = nullptr);
0029     ~PersonsDelegate() override;
0030 
0031     void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
0032     QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
0033 };
0034 
0035 PersonsDelegate::PersonsDelegate(QObject *parent)
0036     : QStyledItemDelegate(parent)
0037 {
0038 }
0039 
0040 PersonsDelegate::~PersonsDelegate()
0041 {
0042 }
0043 
0044 void PersonsDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
0045 {
0046     QStyleOptionViewItem optV4 = option;
0047     initStyleOption(&optV4, index);
0048 
0049     painter->save();
0050 
0051     painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
0052     painter->setClipRect(optV4.rect);
0053 
0054     QStyle *style = QApplication::style();
0055     style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter);
0056 
0057     QRect contactPhotoRect = optV4.rect;
0058     contactPhotoRect.adjust(SPACING, SPACING, SPACING, SPACING);
0059     contactPhotoRect.setWidth(PHOTO_SIZE);
0060     contactPhotoRect.setHeight(PHOTO_SIZE);
0061 
0062     QImage avatar = index.data(Qt::DecorationRole).value<QImage>();
0063     painter->drawImage(contactPhotoRect, avatar);
0064 
0065     painter->drawRect(contactPhotoRect);
0066 
0067     QRect nameRect = optV4.rect;
0068     nameRect.adjust(SPACING + PHOTO_SIZE + SPACING, SPACING, 0, 0);
0069 
0070     painter->drawText(nameRect, index.data(Qt::DisplayRole).toString());
0071 
0072     QRect idRect = optV4.rect;
0073     idRect.adjust(SPACING + PHOTO_SIZE + SPACING, SPACING + 15, 0, 0);
0074     painter->drawText(idRect, index.data(PersonsModel::PersonUriRole).toString());
0075 
0076     painter->restore();
0077 }
0078 
0079 QSize PersonsDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
0080 {
0081     Q_UNUSED(option)
0082     Q_UNUSED(index)
0083     return QSize(128, 48);
0084 }
0085 
0086 class ContactListApp : public QWidget
0087 {
0088     Q_OBJECT
0089 public:
0090     ContactListApp();
0091 private Q_SLOTS:
0092     void onMergeClicked();
0093     void onUnmergeClicked();
0094 
0095 private:
0096     PersonsModel *m_model;
0097     QTreeView *m_view;
0098 };
0099 
0100 ContactListApp::ContactListApp()
0101 {
0102     m_view = new QTreeView(this);
0103     m_model = new PersonsModel(this);
0104 
0105     QVBoxLayout *layout = new QVBoxLayout(this);
0106     QSortFilterProxyModel *sortFilter = new QSortFilterProxyModel(m_view);
0107     sortFilter->setDynamicSortFilter(true);
0108     sortFilter->setSourceModel(m_model);
0109     sortFilter->sort(0);
0110     m_view->setRootIsDecorated(false);
0111     m_view->setModel(sortFilter);
0112     m_view->setItemDelegate(new PersonsDelegate(this));
0113     m_view->setSelectionMode(QAbstractItemView::ExtendedSelection);
0114 
0115     layout->addWidget(m_view);
0116     QPushButton *mergeButton = new QPushButton(QStringLiteral("Merge"), this);
0117     connect(mergeButton, SIGNAL(released()), SLOT(onMergeClicked()));
0118     layout->addWidget(mergeButton);
0119 
0120     QPushButton *unmergeButton = new QPushButton(QStringLiteral("Unmerge"), this);
0121     connect(unmergeButton, SIGNAL(released()), SLOT(onUnmergeClicked()));
0122     layout->addWidget(unmergeButton);
0123 }
0124 
0125 void ContactListApp::onMergeClicked()
0126 {
0127     const QModelIndexList indexes = m_view->selectionModel()->selectedIndexes();
0128     QStringList ids;
0129     for (const QModelIndex &index : indexes) {
0130         ids << index.data(PersonsModel::PersonUriRole).toString();
0131     }
0132 
0133     if (!ids.isEmpty()) {
0134         KPeople::mergeContacts(ids);
0135     }
0136 }
0137 
0138 void ContactListApp::onUnmergeClicked()
0139 {
0140     QModelIndexList indexes = m_view->selectionModel()->selectedIndexes();
0141     if (!indexes.isEmpty()) {
0142         QString id = indexes.first().data(PersonsModel::PersonUriRole).toString();
0143         KPeople::unmergeContact(id);
0144     }
0145 }
0146 
0147 int main(int argc, char **argv)
0148 {
0149     QApplication app(argc, argv);
0150 
0151     ContactListApp widget;
0152     widget.show();
0153     app.exec();
0154 }
0155 
0156 #include "contactlistwidgets.moc"