french -> French
[kdepim.git] / kjots / kjotstreeview.cpp
blobbaf92f09f23dd1aeb5f535a1ce6cbe4c2a7c55a2
1 /*
2 This file is part of KJots.
4 Copyright (c) 2009 Stephen Kelly <steveire@gmail.com>
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.
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.
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.
22 #include "kjotstreeview.h"
24 #include <QApplication>
25 #include <QClipboard>
26 #include <QContextMenuEvent>
27 #include <QMenu>
29 #include <KActionCollection>
30 #include <KXMLGUIClient>
31 #include <KColorDialog>
32 #include <KInputDialog>
33 #include <KLocale>
35 #include <KMime/KMimeMessage>
37 #include "kjotsmodel.h"
39 using namespace Akonadi;
42 KJotsTreeView::KJotsTreeView( KXMLGUIClient* xmlGuiClient, QWidget* parent )
43 : EntityTreeView( xmlGuiClient, parent ), m_xmlGuiClient( xmlGuiClient )
48 void KJotsTreeView::contextMenuEvent(QContextMenuEvent* event)
50 QMenu *popup = new QMenu(this);
52 QModelIndexList rows = selectionModel()->selectedRows();
54 const bool noselection = rows.isEmpty();
55 const bool singleselection = rows.size() == 1;
56 const bool multiselection = rows.size() > 1;
58 popup->addAction(m_xmlGuiClient->actionCollection()->action(QLatin1String("new_book")));
59 if ( singleselection )
61 popup->addAction(m_xmlGuiClient->actionCollection()->action(QLatin1String("new_page")));
62 popup->addAction(m_xmlGuiClient->actionCollection()->action(QLatin1String("rename_entry")));
64 popup->addAction(m_xmlGuiClient->actionCollection()->action(QLatin1String("copy_link_address")));
65 popup->addAction(m_xmlGuiClient->actionCollection()->action(QLatin1String("change_color")));
67 popup->addAction(m_xmlGuiClient->actionCollection()->action(QLatin1String("sort_children_alpha")));
68 popup->addAction(m_xmlGuiClient->actionCollection()->action(QLatin1String("sort_children_by_date")));
70 if ( !noselection )
71 popup->addAction(m_xmlGuiClient->actionCollection()->action(QLatin1String("save_to")));
72 popup->addSeparator();
74 popup->addAction(m_xmlGuiClient->actionCollection()->action(QLatin1String("lock")));
75 popup->addAction(m_xmlGuiClient->actionCollection()->action(QLatin1String("unlock")));
77 if ( singleselection )
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")));
87 if ( multiselection )
88 popup->addAction(m_xmlGuiClient->actionCollection()->action(QLatin1String("del_mult")));
90 popup->exec( event->globalPos() );
92 delete popup;
95 void KJotsTreeView::delayedInitialization()
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()));
105 QString KJotsTreeView::captionForSelection( const QString &sep ) const
107 QString caption;
109 QModelIndexList selection = selectionModel()->selectedRows();
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();
119 if ( parentBook.isValid() ) {
120 caption = sep + index.data().toString() + caption;
121 } else {
122 caption = index.data().toString() + caption;
124 index = parentBook;
127 return caption;
130 void KJotsTreeView::renameEntry()
132 QModelIndexList rows = selectionModel()->selectedRows();
134 if ( rows.size() != 1 )
135 return;
137 QModelIndex idx = rows.at( 0 );
139 QString title = idx.data().toString();
141 Item item = idx.data( KJotsModel::ItemRole ).value<Item>();
142 if ( item.isValid() )
144 Q_ASSERT( item.hasPayload<KMime::Message::Ptr>() );
145 if ( !item.hasPayload<KMime::Message::Ptr>() )
146 return;
148 bool ok;
149 QString name = KInputDialog::getText( i18n( "Rename Page" ),
150 i18n( "Page title:" ), title, &ok, this );
152 if ( ok )
153 model()->setData( idx, name, Qt::EditRole );
154 return;
157 Collection col = idx.data( KJotsModel::CollectionRole ).value<Collection>();
158 Q_ASSERT( col.isValid() );
159 if (!col.isValid())
160 return;
162 bool ok;
163 QString name = KInputDialog::getText( i18n( "Rename Book" ),
164 i18n( "Book name:" ), title, &ok, this );
166 if ( ok )
167 model()->setData( idx, name, Qt::EditRole );
170 void KJotsTreeView::copyLinkAddress()
172 QModelIndexList rows = selectionModel()->selectedRows();
174 if ( rows.size() != 1 )
175 return;
177 QModelIndex idx = rows.at( 0 );
179 QString title = idx.data().toString();
181 Item item = idx.data( KJotsModel::ItemRole ).value<Item>();
183 QMimeData *mimeData = new QMimeData();
185 QString link;
186 if ( item.isValid() )
188 Q_ASSERT( item.hasPayload<KMime::Message::Ptr>() );
189 if ( !item.hasPayload<KMime::Message::Ptr>() )
190 return;
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>();
196 Q_ASSERT(col.isValid());
197 if (!col.isValid())
198 return;
200 link = QString::fromLatin1( "<a href=\"%1\">%2</a>" ).arg( col.url().url() ).arg( title );
203 mimeData->setData( QLatin1String("kjots/internal_link"), link.toUtf8() );
204 mimeData->setText( title );
205 QApplication::clipboard()->setMimeData( mimeData );
208 void KJotsTreeView::changeColor()
210 QColor myColor;
211 int result = KColorDialog::getColor( myColor );
213 if ( result != KColorDialog::Accepted )
214 return;
216 QModelIndexList rows = selectionModel()->selectedRows();
218 foreach ( const QModelIndex &idx, rows )
220 model()->setData(idx, myColor, Qt::BackgroundRole );