]>
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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(DolphinMainWindow
* mainWindow
,
47 SettingsPageBase(parent
),
48 m_mainWindow(mainWindow
),
54 Q3VBoxLayout
* topLayout
= new Q3VBoxLayout(this, 2, KDialog::spacingHint());
56 const int spacing
= KDialog::spacingHint();
58 KHBox
* hBox
= new KHBox(this);
59 hBox
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
60 hBox
->setSpacing(spacing
);
61 hBox
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Ignored
);
63 m_listView
= new K3ListView(hBox
);
64 m_listView
->addColumn(i18n("Icon"));
65 m_listView
->addColumn(i18n("Name"));
66 m_listView
->addColumn(i18n("Location"));
67 m_listView
->setResizeMode(Q3ListView::LastColumn
);
68 m_listView
->setColumnAlignment(0, Qt::AlignHCenter
);
69 m_listView
->setAllColumnsShowFocus(true);
70 m_listView
->setSorting(-1);
71 connect(m_listView
, SIGNAL(selectionChanged()),
72 this, SLOT(updateButtons()));
73 connect(m_listView
, SIGNAL(pressed(Q3ListViewItem
*)),
74 this, SLOT(slotBookmarkPressed(Q3ListViewItem
*)));
75 connect(m_listView
, SIGNAL(doubleClicked(Q3ListViewItem
*, const QPoint
&, int)),
76 this, SLOT(slotBookmarkDoubleClicked(Q3ListViewItem
*, const QPoint
&, int)));
78 KVBox
* buttonBox
= new KVBox(hBox
);
79 buttonBox
->setSpacing(spacing
);
81 const QSizePolicy
buttonSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Maximum
);
83 m_addButton
= new KPushButton(i18n("Add..."), buttonBox
);
84 connect(m_addButton
, SIGNAL(clicked()),
85 this, SLOT(slotAddButtonClicked()));
86 m_addButton
->setSizePolicy(buttonSizePolicy
);
88 m_editButton
= new KPushButton(i18n("Edit..."), buttonBox
);
89 connect(m_editButton
, SIGNAL(clicked()),
90 this, SLOT(slotEditButtonClicked()));
91 m_editButton
->setSizePolicy(buttonSizePolicy
);
93 m_removeButton
= new KPushButton(i18n("Remove"), buttonBox
);
94 connect(m_removeButton
, SIGNAL(clicked()),
95 this, SLOT(slotRemoveButtonClicked()));
96 m_removeButton
->setSizePolicy(buttonSizePolicy
);
98 m_moveUpButton
= new KPushButton(i18n("Move Up"), buttonBox
);
99 connect(m_moveUpButton
, SIGNAL(clicked()),
100 this, SLOT(slotMoveUpButtonClicked()));
101 m_moveUpButton
->setSizePolicy(buttonSizePolicy
);
103 m_moveDownButton
= new KPushButton(i18n("Move Down"), buttonBox
);
104 connect(m_moveDownButton
, SIGNAL(clicked()),
105 this, SLOT(slotMoveDownButtonClicked()));
106 m_moveDownButton
->setSizePolicy(buttonSizePolicy
);
108 // Add a dummy widget with no restriction regarding a vertical resizing.
109 // This assures that the spacing between the buttons is not increased.
110 new QWidget(buttonBox
);
112 topLayout
->addWidget(hBox
);
114 // insert all editable bookmarks.
115 KBookmarkGroup root
= DolphinSettings::instance().bookmarkManager()->root();
116 KBookmark bookmark
= root
.first();
118 Q3ListViewItem
* prev
= 0;
119 while (!bookmark
.isNull()) {
120 Q3ListViewItem
* item
= new Q3ListViewItem(m_listView
);
121 item
->setPixmap(PixmapIdx
, SmallIcon(bookmark
.icon()));
122 item
->setText(NameIdx
, bookmark
.text());
123 item
->setText(UrlIdx
, bookmark
.url().prettyUrl());
125 // add hidden column to be able to retrieve the icon name again
126 item
->setText(IconIdx
, bookmark
.icon());
128 m_listView
->insertItem(item
);
130 item
->moveItem(prev
);
134 bookmark
= root
.next(bookmark
);
136 m_listView
->setSelected(m_listView
->firstChild(), true);
142 BookmarksSettingsPage::~BookmarksSettingsPage()
146 void BookmarksSettingsPage::applySettings()
148 // delete all bookmarks
149 KBookmarkManager
* manager
= DolphinSettings::instance().bookmarkManager();
150 KBookmarkGroup root
= manager
->root();
151 KBookmark bookmark
= root
.first();
152 while (!bookmark
.isNull()) {
153 root
.deleteBookmark(bookmark
);
154 bookmark
= root
.first();
157 // add all items as bookmarks
158 Q3ListViewItem
* item
= m_listView
->firstChild();
160 root
.addBookmark(manager
,
162 KUrl(item
->text(UrlIdx
)),
163 item
->text(IconIdx
)); // hidden column
164 item
= item
->itemBelow();
167 manager
->emitChanged(root
);
170 void BookmarksSettingsPage::updateButtons()
172 const Q3ListViewItem
* selectedItem
= m_listView
->selectedItem();
173 const bool hasSelection
= (selectedItem
!= 0);
175 m_editButton
->setEnabled(hasSelection
);
176 m_removeButton
->setEnabled(hasSelection
);
178 const bool enableMoveUp
= hasSelection
&&
179 (selectedItem
!= m_listView
->firstChild());
180 m_moveUpButton
->setEnabled(enableMoveUp
);
182 const bool enableMoveDown
= hasSelection
&&
183 (selectedItem
!= m_listView
->lastChild());
184 m_moveDownButton
->setEnabled(enableMoveDown
);
187 void BookmarksSettingsPage::slotBookmarkDoubleClicked(Q3ListViewItem
*,
191 slotEditButtonClicked();
194 void BookmarksSettingsPage::slotBookmarkPressed(Q3ListViewItem
* item
)
197 m_listView
->setSelected(m_listView
->currentItem(), true);
201 void BookmarksSettingsPage::slotAddButtonClicked()
203 KBookmark bookmark
= EditBookmarkDialog::getBookmark(i18n("Add Bookmark"),
204 i18n("New bookmark"),
207 if (!bookmark
.isNull()) {
208 // insert bookmark into listview
209 Q3ListViewItem
* item
= new Q3ListViewItem(m_listView
);
210 item
->setPixmap(PixmapIdx
, SmallIcon(bookmark
.icon()));
211 item
->setText(NameIdx
, bookmark
.text());
212 item
->setText(UrlIdx
, bookmark
.url().prettyUrl());
213 item
->setText(IconIdx
, bookmark
.icon());
214 m_listView
->insertItem(item
);
216 Q3ListViewItem
* lastItem
= m_listView
->lastChild();
218 item
->moveItem(lastItem
);
221 m_listView
->setSelected(item
, true);
226 void BookmarksSettingsPage::slotEditButtonClicked()
228 Q3ListViewItem
* item
= m_listView
->selectedItem();
229 assert(item
!= 0); // 'edit' may not get invoked when having no items
231 KBookmark bookmark
= EditBookmarkDialog::getBookmark(i18n("Edit Bookmark"),
233 KUrl(item
->text(UrlIdx
)),
234 item
->text(IconIdx
));
235 if (!bookmark
.isNull()) {
236 item
->setPixmap(PixmapIdx
, SmallIcon(bookmark
.icon()));
237 item
->setText(NameIdx
, bookmark
.text());
238 item
->setText(UrlIdx
, bookmark
.url().prettyUrl());
239 item
->setText(IconIdx
, bookmark
.icon());
243 void BookmarksSettingsPage::slotRemoveButtonClicked()
245 Q3ListViewItem
* selectedItem
= m_listView
->selectedItem();
246 assert(selectedItem
!= 0);
247 Q3ListViewItem
* nextItem
= selectedItem
->itemBelow();
249 nextItem
= selectedItem
->itemAbove();
252 m_listView
->takeItem(selectedItem
);
254 m_listView
->setSelected(nextItem
, true);
258 void BookmarksSettingsPage::slotMoveUpButtonClicked()
263 void BookmarksSettingsPage::slotMoveDownButtonClicked()
268 int BookmarksSettingsPage::selectedBookmarkIndex() const
272 Q3ListViewItem
* selectedItem
= m_listView
->selectedItem();
273 if (selectedItem
!= 0) {
275 Q3ListViewItem
* item
= m_listView
->firstChild();
276 while (item
!= selectedItem
) {
277 item
= item
->nextSibling();
285 void BookmarksSettingsPage::moveBookmark(int direction
)
287 // this implementation currently only allows moving of bookmarks
288 // one step up or down
289 assert((direction
>= -1) && (direction
<= +1));
291 // swap bookmarks in listview
292 Q3ListViewItem
* selectedItem
= m_listView
->selectedItem();
293 assert(selectedItem
!= 0);
294 Q3ListViewItem
* item
= (direction
< 0) ? selectedItem
->itemAbove() :
295 selectedItem
->itemBelow();
299 if (item
->pixmap(0) != 0) {
300 pixmap
= *(item
->pixmap(0));
302 QString
name(item
->text(NameIdx
));
303 QString
url(item
->text(UrlIdx
));
304 QString
icon(item
->text(IconIdx
));
306 if (selectedItem
->pixmap(0) != 0) {
307 item
->setPixmap(PixmapIdx
, *(selectedItem
->pixmap(0)));
309 item
->setText(NameIdx
, selectedItem
->text(NameIdx
));
310 item
->setText(UrlIdx
, selectedItem
->text(UrlIdx
));
311 item
->setText(IconIdx
, selectedItem
->text(IconIdx
));
313 selectedItem
->setPixmap(PixmapIdx
, pixmap
);
314 selectedItem
->setText(NameIdx
, name
);
315 selectedItem
->setText(UrlIdx
, url
);
316 selectedItem
->setText(IconIdx
, icon
);
318 m_listView
->setSelected(item
, true);
321 #include "bookmarkssettingspage.moc"