]> cloud.milkyroute.net Git - dolphin.git/blob - src/bookmarkssidebarpage.cpp
Allow zooming in and zooming out in the icons view.
[dolphin.git] / src / bookmarkssidebarpage.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "bookmarkssidebarpage.h"
21
22 #include <q3listbox.h>
23 #include <qlayout.h>
24 #include <qpainter.h>
25 //Added by qt3to4:
26 #include <QPixmap>
27 #include <Q3VBoxLayout>
28 #include <QPaintEvent>
29 #include <assert.h>
30 #include <q3popupmenu.h>
31
32 #include <kbookmark.h>
33 #include <kbookmarkmanager.h>
34 #include <kmessagebox.h>
35 #include <kiconloader.h>
36 #include <klocale.h>
37
38 #include "dolphinsettings.h"
39 #include "dolphinmainwindow.h"
40 #include "editbookmarkdialog.h"
41
42 BookmarksSidebarPage::BookmarksSidebarPage(DolphinMainWindow* mainWindow, QWidget* parent) :
43 SidebarPage(mainWindow, parent)
44 {
45 Q3VBoxLayout* layout = new Q3VBoxLayout(this);
46 m_bookmarksList = new BookmarksListBox(this);
47 m_bookmarksList->setPaletteBackgroundColor(palette().brush(QPalette::Background).color());
48
49 layout->addWidget(m_bookmarksList);
50 connect(m_bookmarksList, SIGNAL(mouseButtonClicked(int, Q3ListBoxItem*, const QPoint&)),
51 this, SLOT(slotMouseButtonClicked(int, Q3ListBoxItem*)));
52 connect(m_bookmarksList, SIGNAL(contextMenuRequested(Q3ListBoxItem*, const QPoint&)),
53 this, SLOT(slotContextMenuRequested(Q3ListBoxItem*, const QPoint&)));
54
55 KBookmarkManager* manager = DolphinSettings::instance().bookmarkManager();
56 connect(manager, SIGNAL(changed(const QString&, const QString&)),
57 this, SLOT(updateBookmarks()));
58
59 updateBookmarks();
60 }
61
62 BookmarksSidebarPage::~BookmarksSidebarPage()
63 {
64 }
65
66 void BookmarksSidebarPage::activeViewChanged()
67 {
68 connectToActiveView();
69 }
70
71 void BookmarksSidebarPage::updateBookmarks()
72 {
73 m_bookmarksList->clear();
74
75 KIconLoader iconLoader;
76
77 KBookmarkGroup root = DolphinSettings::instance().bookmarkManager()->root();
78 KBookmark bookmark = root.first();
79 while (!bookmark.isNull()) {
80 QPixmap icon(iconLoader.loadIcon(bookmark.icon(),
81 K3Icon::NoGroup,
82 K3Icon::SizeMedium));
83 BookmarkItem* item = new BookmarkItem(icon, bookmark.text());
84 m_bookmarksList->insertItem(item);
85
86 bookmark = root.next(bookmark);
87 }
88
89 connectToActiveView();
90 }
91
92 void BookmarksSidebarPage::slotMouseButtonClicked(int button, Q3ListBoxItem* item)
93 {
94 if ((button != Qt::LeftButton) || (item == 0)) {
95 return;
96 }
97
98 const int index = m_bookmarksList->index(item);
99 KBookmark bookmark = DolphinSettings::instance().bookmark(index);
100 mainWindow()->activeView()->setUrl(bookmark.url());
101 }
102
103 void BookmarksSidebarPage::slotContextMenuRequested(Q3ListBoxItem* item,
104 const QPoint& pos)
105 {
106 const int insertID = 1;
107 const int editID = 2;
108 const int deleteID = 3;
109 const int addID = 4;
110
111 Q3PopupMenu* popup = new Q3PopupMenu();
112 if (item == 0) {
113 popup->insertItem(SmallIcon("filenew"), i18n("Add Bookmark..."), addID);
114 }
115 else {
116 popup->insertItem(SmallIcon("filenew"), i18n("Insert Bookmark..."), insertID);
117 popup->insertItem(SmallIcon("edit"), i18n("Edit..."), editID);
118 popup->insertItem(SmallIcon("editdelete"), i18n("Delete"), deleteID);
119 }
120
121 KBookmarkManager* manager = DolphinSettings::instance().bookmarkManager();
122 KBookmarkGroup root = manager->root();
123 const int index = m_bookmarksList->index(m_bookmarksList->selectedItem());
124
125 const int result = popup->exec(pos);
126 switch (result) {
127 case insertID: {
128 KBookmark newBookmark = EditBookmarkDialog::getBookmark(i18n("Insert Bookmark"),
129 "New bookmark",
130 KUrl(),
131 "bookmark");
132 if (!newBookmark.isNull()) {
133 root.addBookmark(manager, newBookmark);
134 if (index > 0) {
135 KBookmark prevBookmark = DolphinSettings::instance().bookmark(index - 1);
136 root.moveItem(newBookmark, prevBookmark);
137 }
138 else {
139 // insert bookmark at first position (is a little bit tricky as KBookmarkGroup
140 // only allows to move items after existing items)
141 KBookmark firstBookmark = root.first();
142 root.moveItem(newBookmark, firstBookmark);
143 root.moveItem(firstBookmark, newBookmark);
144 }
145 manager->emitChanged(root);
146 }
147 break;
148 }
149
150 case editID: {
151 KBookmark oldBookmark = DolphinSettings::instance().bookmark(index);
152 KBookmark newBookmark = EditBookmarkDialog::getBookmark(i18n("Edit Bookmark"),
153 oldBookmark.text(),
154 oldBookmark.url(),
155 oldBookmark.icon());
156 if (!newBookmark.isNull()) {
157 root.addBookmark(manager, newBookmark);
158 root.moveItem(newBookmark, oldBookmark);
159 root.deleteBookmark(oldBookmark);
160 manager->emitChanged(root);
161 }
162 break;
163 }
164
165 case deleteID: {
166 KBookmark bookmark = DolphinSettings::instance().bookmark(index);
167 root.deleteBookmark(bookmark);
168 manager->emitChanged(root);
169 break;
170 }
171
172 case addID: {
173 KBookmark bookmark = EditBookmarkDialog::getBookmark(i18n("Add Bookmark"),
174 "New bookmark",
175 KUrl(),
176 "bookmark");
177 if (!bookmark.isNull()) {
178 root.addBookmark(manager, bookmark);
179 manager->emitChanged(root);
180 }
181 }
182
183 default: break;
184 }
185
186 delete popup;
187 popup = 0;
188
189 DolphinView* view = mainWindow()->activeView();
190 adjustSelection(view->url());
191 }
192
193
194 void BookmarksSidebarPage::adjustSelection(const KUrl& url)
195 {
196 // TODO (remarked in dolphin/TODO): the following code is quite equal
197 // to BookmarkSelector::updateSelection().
198
199 KBookmarkGroup root = DolphinSettings::instance().bookmarkManager()->root();
200 KBookmark bookmark = root.first();
201
202 int maxLength = 0;
203 int selectedIndex = -1;
204
205 // Search the bookmark which is equal to the Url or at least is a parent Url.
206 // If there are more than one possible parent Url candidates, choose the bookmark
207 // which covers the bigger range of the Url.
208 int i = 0;
209 while (!bookmark.isNull()) {
210 const KUrl bookmarkUrl = bookmark.url();
211 if (bookmarkUrl.isParentOf(url)) {
212 const int length = bookmarkUrl.prettyUrl().length();
213 if (length > maxLength) {
214 selectedIndex = i;
215 maxLength = length;
216 }
217 }
218 bookmark = root.next(bookmark);
219 ++i;
220 }
221
222 const bool block = m_bookmarksList->signalsBlocked();
223 m_bookmarksList->blockSignals(true);
224 if (selectedIndex < 0) {
225 // no bookmark matches, hence deactivate any selection
226 const int currentIndex = m_bookmarksList->index(m_bookmarksList->selectedItem());
227 m_bookmarksList->setSelected(currentIndex, false);
228 }
229 else {
230 // select the bookmark which is part of the current Url
231 m_bookmarksList->setSelected(selectedIndex, true);
232 }
233 m_bookmarksList->blockSignals(block);
234 }
235
236 void BookmarksSidebarPage::slotUrlChanged(const KUrl& url)
237 {
238 adjustSelection(url);
239 }
240
241 void BookmarksSidebarPage::connectToActiveView()
242 {
243 DolphinView* view = mainWindow()->activeView();
244 adjustSelection(view->url());
245 connect(view, SIGNAL(urlChanged(const KUrl&)),
246 this, SLOT(slotUrlChanged(const KUrl&)));
247 }
248
249 BookmarksListBox::BookmarksListBox(QWidget* parent) :
250 Q3ListBox(parent)
251 {
252 }
253 BookmarksListBox::~BookmarksListBox()
254 {
255 }
256
257 void BookmarksListBox::paintEvent(QPaintEvent* /* event */)
258 {
259 // don't invoke QListBox::paintEvent(event) to prevent
260 // that any kind of frame is drawn
261 }
262
263 BookmarkItem::BookmarkItem(const QPixmap& pixmap, const QString& text) :
264 Q3ListBoxPixmap(pixmap, text)
265 {
266 }
267
268 BookmarkItem::~BookmarkItem()
269 {
270 }
271
272 int BookmarkItem::height(const Q3ListBox* listBox) const
273 {
274 return Q3ListBoxPixmap::height(listBox) + 8;
275 }
276
277 #include "bookmarkssidebarpage.moc"