]>
cloud.milkyroute.net Git - dolphin.git/blob - src/bookmarkssidebarpage.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
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. *
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. *
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 ***************************************************************************/
20 #include "bookmarkssidebarpage.h"
22 #include <q3listbox.h>
27 #include <Q3VBoxLayout>
28 #include <QPaintEvent>
32 #include <kbookmark.h>
33 #include <kbookmarkmanager.h>
34 #include <kmessagebox.h>
35 #include <kiconloader.h>
38 #include "dolphinsettings.h"
39 #include "dolphinmainwindow.h"
40 #include "editbookmarkdialog.h"
42 BookmarksSidebarPage::BookmarksSidebarPage(DolphinMainWindow
* mainWindow
, QWidget
* parent
) :
43 SidebarPage(mainWindow
, parent
)
45 Q3VBoxLayout
* layout
= new Q3VBoxLayout(this);
46 m_bookmarksList
= new BookmarksListBox(this);
47 m_bookmarksList
->setPaletteBackgroundColor(palette().brush(QPalette::Background
).color());
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
&)));
55 KBookmarkManager
* manager
= DolphinSettings::instance().bookmarkManager();
56 connect(manager
, SIGNAL(changed(const QString
&, const QString
&)),
57 this, SLOT(updateBookmarks()));
62 BookmarksSidebarPage::~BookmarksSidebarPage()
66 void BookmarksSidebarPage::activeViewChanged()
68 connectToActiveView();
71 void BookmarksSidebarPage::updateBookmarks()
73 m_bookmarksList
->clear();
75 KIconLoader iconLoader
;
77 KBookmarkGroup root
= DolphinSettings::instance().bookmarkManager()->root();
78 KBookmark bookmark
= root
.first();
79 while (!bookmark
.isNull()) {
80 QPixmap
icon(iconLoader
.loadIcon(bookmark
.icon(),
83 BookmarkItem
* item
= new BookmarkItem(icon
, bookmark
.text());
84 m_bookmarksList
->insertItem(item
);
86 bookmark
= root
.next(bookmark
);
89 connectToActiveView();
92 void BookmarksSidebarPage::slotMouseButtonClicked(int button
, Q3ListBoxItem
* item
)
94 if ((button
!= Qt::LeftButton
) || (item
== 0)) {
98 const int index
= m_bookmarksList
->index(item
);
99 KBookmark bookmark
= DolphinSettings::instance().bookmark(index
);
100 mainWindow()->activeView()->setUrl(bookmark
.url());
103 void BookmarksSidebarPage::slotContextMenuRequested(Q3ListBoxItem
* item
,
106 const int insertID
= 1;
107 const int editID
= 2;
108 const int deleteID
= 3;
111 KMenu
* popup
= new KMenu();
113 QAction
*action
= popup
->addAction(SmallIcon("document-new"), i18n("Add Bookmark..."));
114 action
->setData(addID
);
117 QAction
*action
= popup
->addAction(SmallIcon("document-new"), i18n("Insert Bookmark..."));
118 action
->setData(insertID
);
119 action
= popup
->addAction(SmallIcon("edit"), i18n("Edit..."));
120 action
->setData(editID
);
121 action
= popup
->addAction(SmallIcon("edit-delete"), i18n("Delete"));
122 action
->setData(deleteID
);
126 KBookmarkManager
* manager
= DolphinSettings::instance().bookmarkManager();
127 KBookmarkGroup root
= manager
->root();
128 const int index
= m_bookmarksList
->index(m_bookmarksList
->selectedItem());
129 QAction
*result
= popup
->exec(pos
);
132 switch(result
->data().toInt()) {
134 KBookmark newBookmark
= EditBookmarkDialog::getBookmark(i18n("Insert Bookmark"),
138 if (!newBookmark
.isNull()) {
139 root
.addBookmark(manager
, newBookmark
);
141 KBookmark prevBookmark
= DolphinSettings::instance().bookmark(index
- 1);
142 root
.moveItem(newBookmark
, prevBookmark
);
145 // insert bookmark at first position (is a little bit tricky as KBookmarkGroup
146 // only allows to move items after existing items)
147 KBookmark firstBookmark
= root
.first();
148 root
.moveItem(newBookmark
, firstBookmark
);
149 root
.moveItem(firstBookmark
, newBookmark
);
151 manager
->emitChanged(root
);
157 KBookmark oldBookmark
= DolphinSettings::instance().bookmark(index
);
158 KBookmark newBookmark
= EditBookmarkDialog::getBookmark(i18n("Edit Bookmark"),
162 if (!newBookmark
.isNull()) {
163 root
.addBookmark(manager
, newBookmark
);
164 root
.moveItem(newBookmark
, oldBookmark
);
165 root
.deleteBookmark(oldBookmark
);
166 manager
->emitChanged(root
);
172 KBookmark bookmark
= DolphinSettings::instance().bookmark(index
);
173 root
.deleteBookmark(bookmark
);
174 manager
->emitChanged(root
);
179 KBookmark bookmark
= EditBookmarkDialog::getBookmark(i18n("Add Bookmark"),
183 if (!bookmark
.isNull()) {
184 root
.addBookmark(manager
, bookmark
);
185 manager
->emitChanged(root
);
195 DolphinView
* view
= mainWindow()->activeView();
196 adjustSelection(view
->url());
200 void BookmarksSidebarPage::adjustSelection(const KUrl
& url
)
202 // TODO (remarked in dolphin/TODO): the following code is quite equal
203 // to BookmarkSelector::updateSelection().
205 KBookmarkGroup root
= DolphinSettings::instance().bookmarkManager()->root();
206 KBookmark bookmark
= root
.first();
209 int selectedIndex
= -1;
211 // Search the bookmark which is equal to the Url or at least is a parent Url.
212 // If there are more than one possible parent Url candidates, choose the bookmark
213 // which covers the bigger range of the Url.
215 while (!bookmark
.isNull()) {
216 const KUrl bookmarkUrl
= bookmark
.url();
217 if (bookmarkUrl
.isParentOf(url
)) {
218 const int length
= bookmarkUrl
.prettyUrl().length();
219 if (length
> maxLength
) {
224 bookmark
= root
.next(bookmark
);
228 const bool block
= m_bookmarksList
->signalsBlocked();
229 m_bookmarksList
->blockSignals(true);
230 if (selectedIndex
< 0) {
231 // no bookmark matches, hence deactivate any selection
232 const int currentIndex
= m_bookmarksList
->index(m_bookmarksList
->selectedItem());
233 m_bookmarksList
->setSelected(currentIndex
, false);
236 // select the bookmark which is part of the current Url
237 m_bookmarksList
->setSelected(selectedIndex
, true);
239 m_bookmarksList
->blockSignals(block
);
242 void BookmarksSidebarPage::slotUrlChanged(const KUrl
& url
)
244 adjustSelection(url
);
247 void BookmarksSidebarPage::connectToActiveView()
249 DolphinView
* view
= mainWindow()->activeView();
250 adjustSelection(view
->url());
251 connect(view
, SIGNAL(urlChanged(const KUrl
&)),
252 this, SLOT(slotUrlChanged(const KUrl
&)));
255 BookmarksListBox::BookmarksListBox(QWidget
* parent
) :
259 BookmarksListBox::~BookmarksListBox()
263 void BookmarksListBox::paintEvent(QPaintEvent
* /* event */)
265 // don't invoke QListBox::paintEvent(event) to prevent
266 // that any kind of frame is drawn
269 BookmarkItem::BookmarkItem(const QPixmap
& pixmap
, const QString
& text
) :
270 Q3ListBoxPixmap(pixmap
, text
)
274 BookmarkItem::~BookmarkItem()
278 int BookmarkItem::height(const Q3ListBox
* listBox
) const
280 return Q3ListBoxPixmap::height(listBox
) + 8;
283 #include "bookmarkssidebarpage.moc"