]>
cloud.milkyroute.net Git - dolphin.git/blob - src/bookmarkssettingspage.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "bookmarkssettingspage.h"
27 #include <qlineedit.h>
31 #include <Q3VBoxLayout>
33 #include <kbookmark.h>
34 #include <kbookmarkmanager.h>
36 #include <kiconloader.h>
37 #include <k3listview.h>
39 #include <kpushbutton.h>
42 #include "dolphinsettings.h"
43 #include "editbookmarkdialog.h"
45 BookmarksSettingsPage::BookmarksSettingsPage(QWidget
*parent
) :
46 SettingsPageBase(parent
),
52 Q3VBoxLayout
* topLayout
= new Q3VBoxLayout(parent
, 2, KDialog::spacingHint());
54 const int spacing
= KDialog::spacingHint();
56 KHBox
* hBox
= new KHBox(parent
);
57 hBox
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
58 hBox
->setSpacing(spacing
);
59 hBox
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Ignored
);
61 m_listView
= new K3ListView(hBox
);
62 m_listView
->addColumn(i18n("Icon"));
63 m_listView
->addColumn(i18n("Name"));
64 m_listView
->addColumn(i18n("Location"));
65 m_listView
->setResizeMode(Q3ListView::LastColumn
);
66 m_listView
->setColumnAlignment(0, Qt::AlignHCenter
);
67 m_listView
->setAllColumnsShowFocus(true);
68 m_listView
->setSorting(-1);
69 connect(m_listView
, SIGNAL(selectionChanged()),
70 this, SLOT(updateButtons()));
71 connect(m_listView
, SIGNAL(pressed(Q3ListViewItem
*)),
72 this, SLOT(slotBookmarkPressed(Q3ListViewItem
*)));
73 connect(m_listView
, SIGNAL(doubleClicked(Q3ListViewItem
*, const QPoint
&, int)),
74 this, SLOT(slotBookmarkDoubleClicked(Q3ListViewItem
*, const QPoint
&, int)));
76 KVBox
* buttonBox
= new KVBox(hBox
);
77 buttonBox
->setSpacing(spacing
);
79 const QSizePolicy
buttonSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Maximum
);
81 m_addButton
= new KPushButton(i18n("Add..."), buttonBox
);
82 connect(m_addButton
, SIGNAL(clicked()),
83 this, SLOT(slotAddButtonClicked()));
84 m_addButton
->setSizePolicy(buttonSizePolicy
);
86 m_editButton
= new KPushButton(i18n("Edit..."), buttonBox
);
87 connect(m_editButton
, SIGNAL(clicked()),
88 this, SLOT(slotEditButtonClicked()));
89 m_editButton
->setSizePolicy(buttonSizePolicy
);
91 m_removeButton
= new KPushButton(i18n("Remove"), buttonBox
);
92 connect(m_removeButton
, SIGNAL(clicked()),
93 this, SLOT(slotRemoveButtonClicked()));
94 m_removeButton
->setSizePolicy(buttonSizePolicy
);
96 m_moveUpButton
= new KPushButton(i18n("Move Up"), buttonBox
);
97 connect(m_moveUpButton
, SIGNAL(clicked()),
98 this, SLOT(slotMoveUpButtonClicked()));
99 m_moveUpButton
->setSizePolicy(buttonSizePolicy
);
101 m_moveDownButton
= new KPushButton(i18n("Move Down"), buttonBox
);
102 connect(m_moveDownButton
, SIGNAL(clicked()),
103 this, SLOT(slotMoveDownButtonClicked()));
104 m_moveDownButton
->setSizePolicy(buttonSizePolicy
);
106 // Add a dummy widget with no restriction regarding a vertical resizing.
107 // This assures that the spacing between the buttons is not increased.
108 new QWidget(buttonBox
);
110 topLayout
->addWidget(hBox
);
112 // insert all editable bookmarks.
113 KBookmarkGroup root
= DolphinSettings::instance().bookmarkManager()->root();
114 KBookmark bookmark
= root
.first();
116 Q3ListViewItem
* prev
= 0;
117 while (!bookmark
.isNull()) {
118 Q3ListViewItem
* item
= new Q3ListViewItem(m_listView
);
119 item
->setPixmap(PixmapIdx
, SmallIcon(bookmark
.icon()));
120 item
->setText(NameIdx
, bookmark
.text());
121 item
->setText(UrlIdx
, bookmark
.url().prettyUrl());
123 // add hidden column to be able to retrieve the icon name again
124 item
->setText(IconIdx
, bookmark
.icon());
126 m_listView
->insertItem(item
);
128 item
->moveItem(prev
);
132 bookmark
= root
.next(bookmark
);
134 m_listView
->setSelected(m_listView
->firstChild(), true);
140 BookmarksSettingsPage::~BookmarksSettingsPage()
144 void BookmarksSettingsPage::applySettings()
146 // delete all bookmarks
147 KBookmarkManager
* manager
= DolphinSettings::instance().bookmarkManager();
148 KBookmarkGroup root
= manager
->root();
149 KBookmark bookmark
= root
.first();
150 while (!bookmark
.isNull()) {
151 root
.deleteBookmark(bookmark
);
152 bookmark
= root
.first();
155 // add all items as bookmarks
156 Q3ListViewItem
* item
= m_listView
->firstChild();
158 root
.addBookmark(manager
,
160 KUrl(item
->text(UrlIdx
)),
161 item
->text(IconIdx
)); // hidden column
162 item
= item
->itemBelow();
165 manager
->emitChanged(root
);
168 void BookmarksSettingsPage::updateButtons()
170 const Q3ListViewItem
* selectedItem
= m_listView
->selectedItem();
171 const bool hasSelection
= (selectedItem
!= 0);
173 m_editButton
->setEnabled(hasSelection
);
174 m_removeButton
->setEnabled(hasSelection
);
176 const bool enableMoveUp
= hasSelection
&&
177 (selectedItem
!= m_listView
->firstChild());
178 m_moveUpButton
->setEnabled(enableMoveUp
);
180 const bool enableMoveDown
= hasSelection
&&
181 (selectedItem
!= m_listView
->lastChild());
182 m_moveDownButton
->setEnabled(enableMoveDown
);
185 void BookmarksSettingsPage::slotBookmarkDoubleClicked(Q3ListViewItem
*,
189 slotEditButtonClicked();
192 void BookmarksSettingsPage::slotBookmarkPressed(Q3ListViewItem
* item
)
195 m_listView
->setSelected(m_listView
->currentItem(), true);
199 void BookmarksSettingsPage::slotAddButtonClicked()
201 KBookmark bookmark
= EditBookmarkDialog::getBookmark(i18n("Add Bookmark"),
202 i18n("New bookmark"),
205 if (!bookmark
.isNull()) {
206 // insert bookmark into listview
207 Q3ListViewItem
* item
= new Q3ListViewItem(m_listView
);
208 item
->setPixmap(PixmapIdx
, SmallIcon(bookmark
.icon()));
209 item
->setText(NameIdx
, bookmark
.text());
210 item
->setText(UrlIdx
, bookmark
.url().prettyUrl());
211 item
->setText(IconIdx
, bookmark
.icon());
212 m_listView
->insertItem(item
);
214 Q3ListViewItem
* lastItem
= m_listView
->lastChild();
216 item
->moveItem(lastItem
);
219 m_listView
->setSelected(item
, true);
224 void BookmarksSettingsPage::slotEditButtonClicked()
226 Q3ListViewItem
* item
= m_listView
->selectedItem();
227 assert(item
!= 0); // 'edit' may not get invoked when having no items
229 KBookmark bookmark
= EditBookmarkDialog::getBookmark(i18n("Edit Bookmark"),
231 KUrl(item
->text(UrlIdx
)),
232 item
->text(IconIdx
));
233 if (!bookmark
.isNull()) {
234 item
->setPixmap(PixmapIdx
, SmallIcon(bookmark
.icon()));
235 item
->setText(NameIdx
, bookmark
.text());
236 item
->setText(UrlIdx
, bookmark
.url().prettyUrl());
237 item
->setText(IconIdx
, bookmark
.icon());
241 void BookmarksSettingsPage::slotRemoveButtonClicked()
243 Q3ListViewItem
* selectedItem
= m_listView
->selectedItem();
244 assert(selectedItem
!= 0);
245 Q3ListViewItem
* nextItem
= selectedItem
->itemBelow();
247 nextItem
= selectedItem
->itemAbove();
250 m_listView
->takeItem(selectedItem
);
252 m_listView
->setSelected(nextItem
, true);
256 void BookmarksSettingsPage::slotMoveUpButtonClicked()
261 void BookmarksSettingsPage::slotMoveDownButtonClicked()
266 int BookmarksSettingsPage::selectedBookmarkIndex() const
270 Q3ListViewItem
* selectedItem
= m_listView
->selectedItem();
271 if (selectedItem
!= 0) {
273 Q3ListViewItem
* item
= m_listView
->firstChild();
274 while (item
!= selectedItem
) {
275 item
= item
->nextSibling();
283 void BookmarksSettingsPage::moveBookmark(int direction
)
285 // this implementation currently only allows moving of bookmarks
286 // one step up or down
287 assert((direction
>= -1) && (direction
<= +1));
289 // swap bookmarks in listview
290 Q3ListViewItem
* selectedItem
= m_listView
->selectedItem();
291 assert(selectedItem
!= 0);
292 Q3ListViewItem
* item
= (direction
< 0) ? selectedItem
->itemAbove() :
293 selectedItem
->itemBelow();
297 if (item
->pixmap(0) != 0) {
298 pixmap
= *(item
->pixmap(0));
300 QString
name(item
->text(NameIdx
));
301 QString
url(item
->text(UrlIdx
));
302 QString
icon(item
->text(IconIdx
));
304 if (selectedItem
->pixmap(0) != 0) {
305 item
->setPixmap(PixmapIdx
, *(selectedItem
->pixmap(0)));
307 item
->setText(NameIdx
, selectedItem
->text(NameIdx
));
308 item
->setText(UrlIdx
, selectedItem
->text(UrlIdx
));
309 item
->setText(IconIdx
, selectedItem
->text(IconIdx
));
311 selectedItem
->setPixmap(PixmapIdx
, pixmap
);
312 selectedItem
->setText(NameIdx
, name
);
313 selectedItem
->setText(UrlIdx
, url
);
314 selectedItem
->setText(IconIdx
, icon
);
316 m_listView
->setSelected(item
, true);
319 #include "bookmarkssettingspage.moc"