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

messageviewer

  • sources
  • kde-4.14
  • kdepim
  • messageviewer
  • widgets
todoedit.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2014-2015 Montel Laurent <montel@kde.org>
3 
4  This program is free software; you can redistribute it and/or modify it
5  under the terms of the GNU General Public License, version 2, as
6  published by the Free Software Foundation.
7 
8  This program is distributed in the hope that it will be useful, but
9  WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License along
14  with this program; if not, write to the Free Software Foundation, Inc.,
15  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #include "todoedit.h"
19 #include "messageviewer/globalsettings_base.h"
20 
21 #include <KLocalizedString>
22 #include <KLineEdit>
23 #include <KIcon>
24 #include <KPushButton>
25 #include <KMessageWidget>
26 
27 #include <QHBoxLayout>
28 #include <QEvent>
29 #include <QKeyEvent>
30 #include <QLabel>
31 
32 #include <Akonadi/CollectionComboBox>
33 
34 #include <incidenceeditor-ng/incidencedialog.h>
35 #include <incidenceeditor-ng/incidencedialogfactory.h>
36 
37 
38 namespace MessageViewer {
39 MESSAGEVIEWER_EXPORT QAbstractItemModel *_k_todoEditStubModel = 0;
40 }
41 
42 using namespace MessageViewer;
43 
44 TodoEdit::TodoEdit(QWidget *parent)
45  : QWidget(parent)
46 {
47  QVBoxLayout *vbox = new QVBoxLayout;
48  vbox->setMargin(5);
49  vbox->setSpacing(2);
50  setLayout(vbox);
51 
52  mMsgWidget = new KMessageWidget(this);
53  mMsgWidget->setCloseButtonVisible(true);
54  mMsgWidget->setMessageType(KMessageWidget::Positive);
55  mMsgWidget->setObjectName(QLatin1String("msgwidget"));
56  mMsgWidget->setWordWrap(true);
57  mMsgWidget->setVisible(false);
58  vbox->addWidget(mMsgWidget);
59 
60  QHBoxLayout *hbox = new QHBoxLayout;
61  hbox->setMargin(0);
62  hbox->setSpacing(2);
63  vbox->addLayout(hbox);
64 
65  QLabel *lab = new QLabel(i18n("Todo:"));
66  hbox->addWidget(lab);
67 
68  mNoteEdit = new KLineEdit;
69  mNoteEdit->setClearButtonShown(true);
70  mNoteEdit->setObjectName(QLatin1String("noteedit"));
71  mNoteEdit->setFocus();
72  connect(mNoteEdit, SIGNAL(textChanged(QString)), SLOT(slotTextEdited(QString)));
73  connect(mNoteEdit, SIGNAL(returnPressed()), SLOT(slotReturnPressed()));
74  hbox->addWidget(mNoteEdit, 1);
75 
76  hbox->addSpacing(5);
77 
78  mCollectionCombobox = new Akonadi::CollectionComboBox(_k_todoEditStubModel);
79  mCollectionCombobox->setAccessRightsFilter(Akonadi::Collection::CanCreateItem);
80  mCollectionCombobox->setMinimumWidth(250);
81  mCollectionCombobox->setMimeTypeFilter( QStringList() << KCalCore::Todo::todoMimeType() );
82  mCollectionCombobox->setObjectName(QLatin1String("akonadicombobox"));
83 #ifndef QT_NO_ACCESSIBILITY
84  mCollectionCombobox->setAccessibleDescription( i18n("Todo list where the new task will be stored.") );
85 #endif
86  mCollectionCombobox->setToolTip( i18n("Todo list where the new task will be stored.") );
87  connect(mCollectionCombobox, SIGNAL(currentIndexChanged(int)), SLOT(slotCollectionChanged(int)));
88  connect(mCollectionCombobox, SIGNAL(activated(int)), SLOT(slotCollectionChanged(int)));
89  hbox->addWidget(mCollectionCombobox);
90 
91  hbox = new QHBoxLayout;
92  hbox->setMargin(0);
93  hbox->setSpacing(2);
94  vbox->addLayout(hbox);
95 
96  hbox->addStretch(1);
97  mSaveButton = new KPushButton(KIcon(QLatin1String("task-new")), i18n("&Save"));
98  mSaveButton->setObjectName(QLatin1String("save-button"));
99  mSaveButton->setEnabled(false);
100 #ifndef QT_NO_ACCESSIBILITY
101  mSaveButton->setAccessibleDescription(i18n("Create new todo and close this widget."));
102 #endif
103  connect(mSaveButton, SIGNAL(clicked(bool)), this, SLOT(slotReturnPressed()));
104  hbox->addWidget(mSaveButton);
105 
106  mOpenEditorButton = new KPushButton(i18n("Open &editor..."));
107  mOpenEditorButton->setObjectName(QLatin1String("open-editor-button"));
108 #ifndef QT_NO_ACCESSIBILITY
109  mOpenEditorButton->setAccessibleDescription(i18n("Open todo editor, where more details can be changed."));
110 #endif
111  mOpenEditorButton->setEnabled(false);
112  connect(mOpenEditorButton, SIGNAL(clicked(bool)), this, SLOT(slotOpenEditor()));
113  hbox->addWidget(mOpenEditorButton);
114 
115  KPushButton *btn = new KPushButton(KStandardGuiItem::cancel());
116  btn->setObjectName(QLatin1String("close-button"));
117 #ifndef QT_NO_ACCESSIBILITY
118  btn->setAccessibleDescription(i18n("Close the widget for creating new todos."));
119 #endif
120  connect(btn, SIGNAL(clicked(bool)), this, SLOT(slotCloseWidget()));
121  hbox->addWidget(btn);
122 
123 
124  readConfig();
125  setSizePolicy( QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Fixed ) );
126  mCollectionCombobox->installEventFilter(this);
127  installEventFilter(this);
128 }
129 
130 TodoEdit::~TodoEdit()
131 {
132  writeConfig();
133 }
134 
135 void TodoEdit::updateButtons(const QString &subject)
136 {
137  const bool subjectIsNotEmpty = !subject.trimmed().isEmpty();
138  const bool collectionComboboxEmpty = (mCollectionCombobox->count() < 1);
139  mSaveButton->setEnabled(subjectIsNotEmpty && !collectionComboboxEmpty);
140  mOpenEditorButton->setEnabled(subjectIsNotEmpty && !collectionComboboxEmpty);
141 }
142 
143 void TodoEdit::showToDoWidget()
144 {
145  mNoteEdit->setFocus();
146  show();
147 }
148 
149 void TodoEdit::writeConfig()
150 {
151  const Akonadi::Collection col = mCollectionCombobox->currentCollection();
152  // col might not be valid if the collection wasn't found yet (the combo is async), skip saving in that case
153  if (col.isValid() && col.id() != MessageViewer::GlobalSettingsBase::self()->lastSelectedFolder()) {
154  MessageViewer::GlobalSettingsBase::self()->setLastSelectedFolder(col.id());
155  MessageViewer::GlobalSettingsBase::self()->writeConfig();
156  }
157 }
158 
159 void TodoEdit::readConfig()
160 {
161  const qint64 id = MessageViewer::GlobalSettingsBase::self()->lastSelectedFolder();
162  if (id!=-1) {
163  mCollectionCombobox->setDefaultCollection(Akonadi::Collection(id));
164  }
165 }
166 
167 Akonadi::Collection TodoEdit::collection() const
168 {
169  return mCollection;
170 }
171 
172 void TodoEdit::slotCollectionChanged(int /*index*/)
173 {
174  setCollection(mCollectionCombobox->currentCollection());
175 }
176 
177 void TodoEdit::setCollection(const Akonadi::Collection &value)
178 {
179  if (mCollection != value) {
180  mCollection = value;
181  Q_EMIT collectionChanged(mCollection);
182  }
183 }
184 
185 KMime::Message::Ptr TodoEdit::message() const
186 {
187  return mMessage;
188 }
189 
190 void TodoEdit::setMessage(const KMime::Message::Ptr &value)
191 {
192  if (mMessage != value) {
193  mMessage = value;
194  const KMime::Headers::Subject * const subject = mMessage ? mMessage->subject(false) : 0;
195  if (subject) {
196  mNoteEdit->setText(i18n("Reply to \"%1\"", subject->asUnicodeString()));
197  mNoteEdit->selectAll();
198  mNoteEdit->setFocus();
199  } else {
200  mNoteEdit->clear();
201  }
202  Q_EMIT messageChanged(mMessage);
203  }
204 }
205 
206 void TodoEdit::slotCloseWidget()
207 {
208  if (isVisible()) {
209  writeConfig();
210  mNoteEdit->clear();
211  mMessage = KMime::Message::Ptr();
212  mMsgWidget->hide();
213  hide();
214  }
215 }
216 
217 void TodoEdit::slotReturnPressed()
218 {
219  if (!mMessage) {
220  kDebug()<<" Message is null";
221  return;
222  }
223  const Akonadi::Collection collection = mCollectionCombobox->currentCollection();
224  if (!collection.isValid()) {
225  kDebug()<<" Collection is not valid";
226  return;
227  }
228 
229  if (!mNoteEdit->text().trimmed().isEmpty()) {
230  mMsgWidget->setText(i18nc("%1 is summary of the todo, %2 is name of the folder in which it is stored",
231  "New todo '%1' was added to task list '%2'", mNoteEdit->text(), collection.displayName()));
232  KCalCore::Todo::Ptr todo( new KCalCore::Todo );
233  todo->setSummary(mNoteEdit->text());
234  mNoteEdit->clear();
235 
236  // We don't hide the widget here, so that multiple todo's can be added
237  Q_EMIT createTodo(todo, collection);
238 
239  mMsgWidget->animatedShow();
240  }
241 }
242 
243 bool TodoEdit::eventFilter(QObject *object, QEvent *e)
244 {
245  // Close the bar when pressing Escape.
246  // Not using a QShortcut for this because it could conflict with
247  // window-global actions (e.g. Emil Sedgh binds Esc to "close tab").
248  // With a shortcut override we can catch this before it gets to kactions.
249  const bool shortCutOverride = (e->type() == QEvent::ShortcutOverride);
250  if (shortCutOverride || e->type() == QEvent::KeyPress ) {
251  QKeyEvent* kev = static_cast<QKeyEvent* >(e);
252  if (kev->key() == Qt::Key_Escape) {
253  e->accept();
254  slotCloseWidget();
255  return true;
256  } else if ( kev->key() == Qt::Key_Enter ||
257  kev->key() == Qt::Key_Return ||
258  kev->key() == Qt::Key_Space) {
259  e->accept();
260  if ( shortCutOverride ) {
261  return true;
262  }
263  if (object == mCollectionCombobox) {
264  mCollectionCombobox->showPopup();
265  return true;
266  }
267  }
268  }
269  return QWidget::eventFilter(object,e);
270 }
271 
272 void TodoEdit::slotOpenEditor()
273 {
274  const KMime::Headers::Subject * const subject = mMessage->subject(false);
275  IncidenceEditorNG::IncidenceDialog *dlg = IncidenceEditorNG::IncidenceDialogFactory::createTodoEditor(
276  mNoteEdit->text(), QString(),
277  QStringList() << QString::fromLatin1(mMessage->encodedContent().toBase64()),
278  QStringList(), // attendees
279  QStringList() << KMime::Message::mimeType(),
280  QStringList() << (subject ? subject->asUnicodeString() : QString()),
281  false, mCollection, false, this);
282  connect(dlg, SIGNAL(finished()), this, SLOT(slotCloseWidget()));
283  dlg->open();
284 }
285 
286 void TodoEdit::slotTextEdited(const QString &subject)
287 {
288  updateButtons(subject);
289  if (mMsgWidget->isVisible()) {
290  mMsgWidget->hide();
291  }
292 }
MessageViewer::TodoEdit::TodoEdit
TodoEdit(QWidget *parent=0)
Definition: todoedit.cpp:44
QEvent
QWidget
QEvent::type
Type type() const
MessageViewer::TodoEdit::messageChanged
void messageChanged(const KMime::Message::Ptr &msg)
QSizePolicy
MessageViewer::TodoEdit::setCollection
void setCollection(const Akonadi::Collection &value)
Definition: todoedit.cpp:177
QWidget::isVisible
bool isVisible() const
MessageViewer::TodoEdit::~TodoEdit
~TodoEdit()
Definition: todoedit.cpp:130
QHBoxLayout
MessageViewer::TodoEdit::writeConfig
void writeConfig()
Definition: todoedit.cpp:149
QBoxLayout::addSpacing
void addSpacing(int size)
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QWidget::setLayout
void setLayout(QLayout *layout)
QObject::installEventFilter
void installEventFilter(QObject *filterObj)
todoedit.h
MessageViewer::TodoEdit::slotCloseWidget
void slotCloseWidget()
Definition: todoedit.cpp:206
QObject
QString::isEmpty
bool isEmpty() const
QString::trimmed
QString trimmed() const
MessageViewer::TodoEdit::collectionChanged
void collectionChanged(const Akonadi::Collection &col)
MessageViewer::TodoEdit::eventFilter
bool eventFilter(QObject *object, QEvent *e)
Definition: todoedit.cpp:243
QVBoxLayout
QObject::eventFilter
virtual bool eventFilter(QObject *watched, QEvent *event)
QString
QWidget::hide
void hide()
QLayout::setMargin
void setMargin(int margin)
QWidget::setSizePolicy
void setSizePolicy(QSizePolicy)
QStringList
QKeyEvent::key
int key() const
QEvent::accept
void accept()
MESSAGEVIEWER_EXPORT
#define MESSAGEVIEWER_EXPORT
Definition: messageviewer_export.h:37
QKeyEvent
QLatin1String
QBoxLayout::addStretch
void addStretch(int stretch)
MessageViewer::TodoEdit::setMessage
void setMessage(const KMime::Message::Ptr &value)
Definition: todoedit.cpp:190
MessageViewer::TodoEdit::message
KMime::Message::Ptr message() const
Definition: todoedit.cpp:185
MessageViewer::_k_todoEditStubModel
MESSAGEVIEWER_EXPORT QAbstractItemModel * _k_todoEditStubModel
Definition: todoedit.cpp:39
QAbstractItemModel
QString::fromLatin1
QString fromLatin1(const char *str, int size)
QWidget::show
void show()
MessageViewer::TodoEdit::createTodo
void createTodo(const KCalCore::Todo::Ptr &todo, const Akonadi::Collection &collection)
MessageViewer::TodoEdit::collection
Akonadi::Collection collection() const
Definition: todoedit.cpp:167
KMessageWidget
MessageViewer::TodoEdit::showToDoWidget
void showToDoWidget()
Definition: todoedit.cpp:143
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QLabel
QBoxLayout::setSpacing
void setSpacing(int spacing)
QBoxLayout::addLayout
void addLayout(QLayout *layout, int stretch)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:32:45 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

messageviewer

Skip menu "messageviewer"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

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