• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kontact

  • sources
  • kde-4.14
  • kdepim
  • kontact
  • plugins
  • knotes
knotes/summarywidget.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Kontact.
3 
4  Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>
5  Copyright (c) 2014-2015 Laurent Montel <montel@kde.org>
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License along
18  with this program; if not, write to the Free Software Foundation, Inc.,
19  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 
21  As a special exception, permission is given to link this program
22  with any edition of Qt, and distribute the resulting executable,
23  without including the source code for Qt in the source distribution.
24 */
25 
26 #include "summarywidget.h"
27 #include "knotes_plugin.h"
28 #include "knotesinterface.h"
29 
30 #include "noteshared/akonadi/notesakonaditreemodel.h"
31 #include "noteshared/akonadi/noteschangerecorder.h"
32 #include "noteshared/attributes/showfoldernotesattribute.h"
33 #include "noteshared/attributes/notedisplayattribute.h"
34 
35 #include <Akonadi/Item>
36 #include <Akonadi/Session>
37 #include <Akonadi/ChangeRecorder>
38 #include <Akonadi/ETMViewStateSaver>
39 #include <Akonadi/CollectionStatistics>
40 #include <KCheckableProxyModel>
41 
42 #include <KMime/KMimeMessage>
43 
44 #include <KontactInterface/Core>
45 #include <KontactInterface/Plugin>
46 
47 #include <KIconLoader>
48 #include <KLocalizedString>
49 #include <KUrlLabel>
50 #include <KMenu>
51 #include <KIconEffect>
52 
53 
54 #include <QEvent>
55 #include <QGridLayout>
56 #include <QLabel>
57 #include <QVBoxLayout>
58 #include <QItemSelectionModel>
59 
60 
61 KNotesSummaryWidget::KNotesSummaryWidget(KontactInterface::Plugin *plugin, QWidget *parent )
62  : KontactInterface::Summary( parent ),
63  mLayout( 0 ),
64  mPlugin( plugin ),
65  mInProgress( false )
66 {
67  mDefaultPixmap = KIconLoader::global()->loadIcon( QLatin1String("knotes"), KIconLoader::Desktop );
68  QVBoxLayout *mainLayout = new QVBoxLayout( this );
69  mainLayout->setSpacing( 3 );
70  mainLayout->setMargin( 3 );
71 
72  QWidget *header = createHeader( this, QLatin1String("view-pim-notes"), i18n( "Popup Notes" ) );
73  mainLayout->addWidget( header );
74 
75  mLayout = new QGridLayout();
76  mainLayout->addItem( mLayout );
77  mLayout->setSpacing( 3 );
78  mLayout->setRowStretch( 6, 1 );
79 
80  KIconLoader loader( QLatin1String("knotes") );
81 
82  mPixmap = loader.loadIcon( QLatin1String("knotes"), KIconLoader::Small );
83 
84  Akonadi::Session *session = new Akonadi::Session( "KNotes Session", this );
85  mNoteRecorder = new NoteShared::NotesChangeRecorder(this);
86  mNoteRecorder->changeRecorder()->setSession(session);
87  mNoteTreeModel = new NoteShared::NotesAkonadiTreeModel(mNoteRecorder->changeRecorder(), this);
88 
89  connect( mNoteTreeModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
90  SLOT(updateFolderList()));
91 
92  connect( mNoteRecorder->changeRecorder(), SIGNAL(itemChanged(Akonadi::Item,QSet<QByteArray>)), SLOT(updateFolderList()));
93  connect( mNoteRecorder->changeRecorder(), SIGNAL(itemRemoved(Akonadi::Item)), SLOT(updateFolderList()) );
94 
95  mSelectionModel = new QItemSelectionModel( mNoteTreeModel );
96  mModelProxy = new KCheckableProxyModel( this );
97  mModelProxy->setSelectionModel( mSelectionModel );
98  mModelProxy->setSourceModel( mNoteTreeModel );
99 
100  KSharedConfigPtr _config = KSharedConfig::openConfig( QLatin1String("kcmknotessummaryrc") );
101 
102  mModelState =
103  new KViewStateMaintainer<Akonadi::ETMViewStateSaver>( _config->group( "CheckState" ), this );
104  mModelState->setSelectionModel( mSelectionModel );
105 }
106 
107 KNotesSummaryWidget::~KNotesSummaryWidget()
108 {
109 }
110 
111 void KNotesSummaryWidget::updateFolderList()
112 {
113  if (mInProgress)
114  return;
115  mInProgress = true;
116  qDeleteAll( mLabels );
117  mLabels.clear();
118  int counter = 0;
119 
120  mModelState->restoreState();
121  displayNotes(QModelIndex(), counter);
122  mInProgress = false;
123 
124  if ( counter == 0 ) {
125  QLabel *label = new QLabel( i18n( "No note found" ), this );
126  label->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
127  mLayout->addWidget( label, 0, 0 );
128  mLabels.append( label );
129  }
130  QList<QLabel*>::const_iterator lit;
131  QList<QLabel*>::const_iterator lend( mLabels.constEnd() );
132  for ( lit = mLabels.constBegin(); lit != lend; ++lit ) {
133  (*lit)->show();
134  }
135 }
136 
137 void KNotesSummaryWidget::displayNotes( const QModelIndex &parent, int &counter)
138 {
139  const int nbCol = mModelProxy->rowCount( parent );
140  for ( int i = 0; i < nbCol; ++i ) {
141  const QModelIndex child = mModelProxy->index( i, 0, parent );
142  const Akonadi::Item item =
143  mModelProxy->data( child,
144  Akonadi::EntityTreeModel::ItemRole ).value<Akonadi::Item>();
145  if (item.isValid()) {
146  createNote(item, counter);
147  ++counter;
148  }
149  displayNotes( child, counter );
150  }
151 }
152 
153 void KNotesSummaryWidget::slotPopupMenu(const QString &note)
154 {
155  KMenu popup( this );
156  const QAction *modifyNoteAction = popup.addAction(
157  KIconLoader::global()->loadIcon( QLatin1String("document-edit"), KIconLoader::Small ),
158  i18n( "Modify Note..." ) );
159  popup.addSeparator();
160  const QAction *deleteNoteAction = popup.addAction(
161  KIconLoader::global()->loadIcon( QLatin1String("edit-delete"), KIconLoader::Small ),
162  i18n( "Delete Note..." ) );
163 
164  const QAction *ret = popup.exec( QCursor::pos() );
165  if ( ret == deleteNoteAction ) {
166  deleteNote( note );
167  } else if ( ret == modifyNoteAction ) {
168  slotSelectNote( note );
169  }
170 }
171 
172 void KNotesSummaryWidget::deleteNote(const QString &note)
173 {
174  org::kde::kontact::KNotes knotes( QLatin1String("org.kde.kontact"), QLatin1String("/KNotes"), QDBusConnection::sessionBus() );
175  knotes.killNote(note.toLongLong());
176 }
177 
178 void KNotesSummaryWidget::createNote(const Akonadi::Item &item, int counter)
179 {
180  if (!item.hasPayload<KMime::Message::Ptr>())
181  return;
182 
183  KMime::Message::Ptr noteMessage = item.payload<KMime::Message::Ptr>();
184  if (!noteMessage)
185  return;
186  const KMime::Headers::Subject * const subject = noteMessage->subject(false);
187  KUrlLabel *urlLabel = new KUrlLabel( QString::number( item.id() ), subject ? subject->asUnicodeString() : QString(), this );
188 
189  urlLabel->installEventFilter( this );
190  urlLabel->setAlignment( Qt::AlignLeft );
191  urlLabel->setWordWrap( true );
192  connect( urlLabel, SIGNAL(leftClickedUrl(QString)), this, SLOT(slotSelectNote(QString)) );
193  connect( urlLabel, SIGNAL(rightClickedUrl(QString)), this, SLOT(slotPopupMenu(QString)) );
194 
195  mLayout->addWidget( urlLabel, counter, 1 );
196 
197  QColor color;
198  if ( item.hasAttribute<NoteShared::NoteDisplayAttribute>()) {
199  color = item.attribute<NoteShared::NoteDisplayAttribute>()->backgroundColor();
200  }
201  // Folder icon.
202  KIconEffect effect;
203  QPixmap pixmap = effect.apply( mDefaultPixmap, KIconEffect::Colorize, 1, color, false );
204 
205  QLabel *label = new QLabel( this );
206  label->setAlignment( Qt::AlignVCenter );
207  QIcon icon(pixmap);
208  label->setPixmap( icon.pixmap( label->height() / 1.5 ) );
209  label->setMaximumWidth( label->minimumSizeHint().width() );
210  mLayout->addWidget( label, counter, 0 );
211  mLabels.append( label );
212  mLabels.append( urlLabel );
213 }
214 
215 void KNotesSummaryWidget::updateSummary( bool force )
216 {
217  Q_UNUSED( force );
218  updateFolderList();
219 }
220 
221 void KNotesSummaryWidget::slotSelectNote( const QString &note )
222 {
223  if ( !mPlugin->isRunningStandalone() ) {
224  mPlugin->core()->selectPlugin( mPlugin );
225  } else {
226  mPlugin->bringToForeground();
227  }
228  org::kde::kontact::KNotes knotes( QLatin1String("org.kde.kontact"), QLatin1String("/KNotes"), QDBusConnection::sessionBus() );
229  knotes.editNote(note.toLongLong());
230 }
231 
232 bool KNotesSummaryWidget::eventFilter( QObject *obj, QEvent *e )
233 {
234  if ( obj->inherits( "KUrlLabel" ) ) {
235  KUrlLabel* label = static_cast<KUrlLabel*>( obj );
236  if ( e->type() == QEvent::Enter ) {
237  emit message( i18n( "Read Popup Note: \"%1\"", label->text() ) );
238  } else if ( e->type() == QEvent::Leave ) {
239  emit message( QString::null ); //krazy:exclude=nullstrassign for old broken gcc
240  }
241  }
242 
243  return KontactInterface::Summary::eventFilter( obj, e );
244 }
245 
246 
247 QStringList KNotesSummaryWidget::configModules() const
248 {
249  return QStringList()<<QLatin1String( "kcmknotessummary.desktop" );
250 }
QList::clear
void clear()
QModelIndex
QEvent
QWidget
QEvent::type
Type type() const
QSize::width
int width() const
QGridLayout::addWidget
void addWidget(QWidget *widget, int row, int column, QFlags< Qt::AlignmentFlag > alignment)
QLabel::setPixmap
void setPixmap(const QPixmap &)
QLabel::setAlignment
void setAlignment(QFlags< Qt::AlignmentFlag >)
QDBusConnection::sessionBus
QDBusConnection sessionBus()
QGridLayout
KNotesSummaryWidget::KNotesSummaryWidget
KNotesSummaryWidget(KontactInterface::Plugin *plugin, QWidget *parent)
Definition: knotes/summarywidget.cpp:61
QGridLayout::setSpacing
void setSpacing(int spacing)
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QString::number
QString number(int n, int base)
QList::append
void append(const T &value)
knotes_plugin.h
QGridLayout::setRowStretch
void setRowStretch(int row, int stretch)
QObject::inherits
bool inherits(const char *className) const
QObject
QBoxLayout::addItem
virtual void addItem(QLayoutItem *item)
subject
QString subject() const
KNotesSummaryWidget::configModules
QStringList configModules() const
Definition: knotes/summarywidget.cpp:247
QVBoxLayout
QSet
QString
QList
QColor
QLayout::setMargin
void setMargin(int margin)
QStringList
QPixmap
KNotesSummaryWidget::eventFilter
virtual bool eventFilter(QObject *obj, QEvent *e)
Definition: knotes/summarywidget.cpp:232
QWidget::setMaximumWidth
void setMaximumWidth(int maxw)
QCursor::pos
QPoint pos()
QLatin1String
QAction
QLabel::minimumSizeHint
virtual QSize minimumSizeHint() const
KNotesSummaryWidget::~KNotesSummaryWidget
~KNotesSummaryWidget()
Definition: knotes/summarywidget.cpp:107
QList::constEnd
const_iterator constEnd() const
QList::constBegin
const_iterator constBegin() const
QItemSelectionModel
QLabel
KNotesSummaryWidget::updateSummary
void updateSummary(bool force=false)
Definition: knotes/summarywidget.cpp:215
QString::toLongLong
qlonglong toLongLong(bool *ok, int base) const
QBoxLayout::setSpacing
void setSpacing(int spacing)
QWidget::height
height
QIcon
summarywidget.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:34:11 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kontact

Skip menu "kontact"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal