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

kjots

  • sources
  • kde-4.14
  • kdepim
  • kjots
kjotstreeview.cpp
Go to the documentation of this file.
1 /*
2  This file is part of KJots.
3 
4  Copyright (c) 2009 Stephen Kelly <steveire@gmail.com>
5 
6  This library is free software; you can redistribute it and/or modify it
7  under the terms of the GNU Library General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or (at your
9  option) any later version.
10 
11  This library is distributed in the hope that it will be useful, but WITHOUT
12  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14  License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301, USA.
20 */
21 
22 #include "kjotstreeview.h"
23 
24 #include <QApplication>
25 #include <QClipboard>
26 #include <QContextMenuEvent>
27 #include <QMenu>
28 
29 #include <KActionCollection>
30 #include <KXMLGUIClient>
31 #include <KColorDialog>
32 #include <KInputDialog>
33 #include <KLocalizedString>
34 
35 #include <KMime/KMimeMessage>
36 
37 #include "kjotsmodel.h"
38 
39 using namespace Akonadi;
40 
41 
42 KJotsTreeView::KJotsTreeView( KXMLGUIClient* xmlGuiClient, QWidget* parent )
43  : EntityTreeView( xmlGuiClient, parent ), m_xmlGuiClient( xmlGuiClient )
44 {
45 
46 }
47 
48 void KJotsTreeView::contextMenuEvent(QContextMenuEvent* event)
49 {
50  QMenu *popup = new QMenu(this);
51 
52  QModelIndexList rows = selectionModel()->selectedRows();
53 
54  const bool noselection = rows.isEmpty();
55  const bool singleselection = rows.size() == 1;
56  const bool multiselection = rows.size() > 1;
57 
58  popup->addAction(m_xmlGuiClient->actionCollection()->action(QLatin1String("new_book")));
59  if ( singleselection )
60  {
61  popup->addAction(m_xmlGuiClient->actionCollection()->action(QLatin1String("new_page")));
62  popup->addAction(m_xmlGuiClient->actionCollection()->action(QLatin1String("rename_entry")));
63 
64  popup->addAction(m_xmlGuiClient->actionCollection()->action(QLatin1String("copy_link_address")));
65  popup->addAction(m_xmlGuiClient->actionCollection()->action(QLatin1String("change_color")));
66 
67  popup->addAction(m_xmlGuiClient->actionCollection()->action(QLatin1String("sort_children_alpha")));
68  popup->addAction(m_xmlGuiClient->actionCollection()->action(QLatin1String("sort_children_by_date")));
69  }
70  if ( !noselection )
71  popup->addAction(m_xmlGuiClient->actionCollection()->action(QLatin1String("save_to")));
72  popup->addSeparator();
73 
74  popup->addAction(m_xmlGuiClient->actionCollection()->action(QLatin1String("lock")));
75  popup->addAction(m_xmlGuiClient->actionCollection()->action(QLatin1String("unlock")));
76 
77  if ( singleselection )
78  {
79  Item item = rows.at( 0 ).data( KJotsModel::ItemRole ).value<Item>();
80  if ( item.isValid() ) {
81  popup->addAction(m_xmlGuiClient->actionCollection()->action(QLatin1String("del_page")));
82  } else {
83  popup->addAction(m_xmlGuiClient->actionCollection()->action(QLatin1String("del_folder")));
84  }
85  }
86 
87  if ( multiselection )
88  popup->addAction(m_xmlGuiClient->actionCollection()->action(QLatin1String("del_mult")));
89 
90  popup->exec( event->globalPos() );
91 
92  delete popup;
93 }
94 
95 void KJotsTreeView::delayedInitialization()
96 {
97  connect(m_xmlGuiClient->actionCollection()->action(QLatin1String("rename_entry")), SIGNAL(triggered()),
98  this, SLOT(renameEntry()));
99  connect(m_xmlGuiClient->actionCollection()->action(QLatin1String("copy_link_address")), SIGNAL(triggered()),
100  this, SLOT(copyLinkAddress()));
101  connect(m_xmlGuiClient->actionCollection()->action(QLatin1String("change_color")), SIGNAL(triggered()),
102  this, SLOT(changeColor()));
103 }
104 
105 QString KJotsTreeView::captionForSelection( const QString &sep ) const
106 {
107  QString caption;
108 
109  QModelIndexList selection = selectionModel()->selectedRows();
110 
111  int selectionSize = selection.size();
112  if ( selectionSize > 1 ) {
113  caption = i18n("Multiple selections");
114  } else if ( selectionSize == 1 ) {
115  QModelIndex index = selection.at( 0 );
116  while ( index.isValid() ) {
117  QModelIndex parentBook = index.parent();
118 
119  if ( parentBook.isValid() ) {
120  caption = sep + index.data().toString() + caption;
121  } else {
122  caption = index.data().toString() + caption;
123  }
124  index = parentBook;
125  }
126  }
127  return caption;
128 }
129 
130 void KJotsTreeView::renameEntry()
131 {
132  QModelIndexList rows = selectionModel()->selectedRows();
133 
134  if ( rows.size() != 1 )
135  return;
136 
137  QModelIndex idx = rows.at( 0 );
138 
139  QString title = idx.data().toString();
140 
141  Item item = idx.data( KJotsModel::ItemRole ).value<Item>();
142  if ( item.isValid() )
143  {
144  Q_ASSERT( item.hasPayload<KMime::Message::Ptr>() );
145  if ( !item.hasPayload<KMime::Message::Ptr>() )
146  return;
147 
148  bool ok;
149  QString name = KInputDialog::getText( i18n( "Rename Page" ),
150  i18n( "Page title:" ), title, &ok, this );
151 
152  if ( ok )
153  model()->setData( idx, name, Qt::EditRole );
154  return;
155  }
156 
157  Collection col = idx.data( KJotsModel::CollectionRole ).value<Collection>();
158  Q_ASSERT( col.isValid() );
159  if (!col.isValid())
160  return;
161 
162  bool ok;
163  QString name = KInputDialog::getText( i18n( "Rename Book" ),
164  i18n( "Book name:" ), title, &ok, this );
165 
166  if ( ok )
167  model()->setData( idx, name, Qt::EditRole );
168 }
169 
170 void KJotsTreeView::copyLinkAddress()
171 {
172  QModelIndexList rows = selectionModel()->selectedRows();
173 
174  if ( rows.size() != 1 )
175  return;
176 
177  QModelIndex idx = rows.at( 0 );
178 
179  QString title = idx.data().toString();
180 
181  Item item = idx.data( KJotsModel::ItemRole ).value<Item>();
182 
183  QMimeData *mimeData = new QMimeData();
184 
185  QString link;
186  if ( item.isValid() )
187  {
188  Q_ASSERT( item.hasPayload<KMime::Message::Ptr>() );
189  if ( !item.hasPayload<KMime::Message::Ptr>() )
190  return;
191 
192  link = QString::fromLatin1( "<a href=\"%1\">%2</a>" ).arg( item.url().url() ).arg( title );
193  } else {
194  Collection col = idx.data( KJotsModel::CollectionRole ).value<Collection>();
195 
196  Q_ASSERT(col.isValid());
197  if (!col.isValid())
198  return;
199 
200  link = QString::fromLatin1( "<a href=\"%1\">%2</a>" ).arg( col.url().url() ).arg( title );
201  }
202 
203  mimeData->setData( QLatin1String("kjots/internal_link"), link.toUtf8() );
204  mimeData->setText( title );
205  QApplication::clipboard()->setMimeData( mimeData );
206 }
207 
208 void KJotsTreeView::changeColor()
209 {
210  QColor myColor;
211  int result = KColorDialog::getColor( myColor );
212 
213  if ( result != KColorDialog::Accepted )
214  return;
215 
216  QModelIndexList rows = selectionModel()->selectedRows();
217 
218  foreach ( const QModelIndex &idx, rows )
219  {
220  model()->setData(idx, myColor, Qt::BackgroundRole );
221  }
222 }
223 
QModelIndex
QWidget
KJotsTreeView::changeColor
void changeColor()
Definition: kjotstreeview.cpp:208
KJotsTreeView::renameEntry
void renameEntry()
Definition: kjotstreeview.cpp:130
QString::size
int size() const
QMenu::addAction
void addAction(QAction *action)
QVariant::value
T value() const
QContextMenuEvent::globalPos
const QPoint & globalPos() const
QMimeData
kjotstreeview.h
KJotsTreeView::copyLinkAddress
void copyLinkAddress()
Definition: kjotstreeview.cpp:170
KJotsTreeView::captionForSelection
QString captionForSelection(const QString &sep) const
Definition: kjotstreeview.cpp:105
QModelIndex::isValid
bool isValid() const
QApplication::clipboard
QClipboard * clipboard()
QContextMenuEvent
KJotsTreeView::KJotsTreeView
KJotsTreeView(KXMLGUIClient *xmlGuiClient, QWidget *parent=0)
Definition: kjotstreeview.cpp:42
QMenu::addSeparator
QAction * addSeparator()
QString
QColor
QClipboard::setMimeData
void setMimeData(QMimeData *src, Mode mode)
QModelIndex::parent
QModelIndex parent() const
QMenu::exec
QAction * exec()
QMenu
QModelIndex::data
QVariant data(int role) const
QLatin1String
KJotsTreeView::contextMenuEvent
virtual void contextMenuEvent(QContextMenuEvent *event)
Definition: kjotstreeview.cpp:48
kjotsmodel.h
QString::fromLatin1
QString fromLatin1(const char *str, int size)
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QVariant::toString
QString toString() const
KJotsTreeView::delayedInitialization
void delayedInitialization()
Definition: kjotstreeview.cpp:95
QString::toUtf8
QByteArray toUtf8() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:32:12 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kjots

Skip menu "kjots"
  • 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