]> cloud.milkyroute.net Git - dolphin.git/blob - src/bookmarkssettingspage.cpp
Initial step for moving to KDirModel. Large code parts have been deleted, as a step...
[dolphin.git] / src / bookmarkssettingspage.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz *
3 * peter.penz@gmx.at *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20
21 #include "bookmarkssettingspage.h"
22
23 #include <assert.h>
24
25 #include <qlayout.h>
26 #include <qlabel.h>
27 #include <qlineedit.h>
28
29 //Added by qt3to4:
30 #include <QPixmap>
31 #include <Q3VBoxLayout>
32
33 #include <kbookmark.h>
34 #include <kbookmarkmanager.h>
35 #include <kdialog.h>
36 #include <kiconloader.h>
37 #include <k3listview.h>
38 #include <klocale.h>
39 #include <kpushbutton.h>
40 #include <kvbox.h>
41
42 #include "dolphinsettings.h"
43 #include "editbookmarkdialog.h"
44
45 BookmarksSettingsPage::BookmarksSettingsPage(QWidget*parent) :
46 SettingsPageBase(parent),
47 m_addButton(0),
48 m_removeButton(0),
49 m_moveUpButton(0),
50 m_moveDownButton(0)
51 {
52 Q3VBoxLayout* topLayout = new Q3VBoxLayout(parent, 2, KDialog::spacingHint());
53
54 const int spacing = KDialog::spacingHint();
55
56 KHBox* hBox = new KHBox(parent);
57 hBox->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
58 hBox->setSpacing(spacing);
59 hBox->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Ignored);
60
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)));
75
76 KVBox* buttonBox = new KVBox(hBox);
77 buttonBox->setSpacing(spacing);
78
79 const QSizePolicy buttonSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
80
81 m_addButton = new KPushButton(i18n("Add..."), buttonBox);
82 connect(m_addButton, SIGNAL(clicked()),
83 this, SLOT(slotAddButtonClicked()));
84 m_addButton->setSizePolicy(buttonSizePolicy);
85
86 m_editButton = new KPushButton(i18n("Edit..."), buttonBox);
87 connect(m_editButton, SIGNAL(clicked()),
88 this, SLOT(slotEditButtonClicked()));
89 m_editButton->setSizePolicy(buttonSizePolicy);
90
91 m_removeButton = new KPushButton(i18n("Remove"), buttonBox);
92 connect(m_removeButton, SIGNAL(clicked()),
93 this, SLOT(slotRemoveButtonClicked()));
94 m_removeButton->setSizePolicy(buttonSizePolicy);
95
96 m_moveUpButton = new KPushButton(i18n("Move Up"), buttonBox);
97 connect(m_moveUpButton, SIGNAL(clicked()),
98 this, SLOT(slotMoveUpButtonClicked()));
99 m_moveUpButton->setSizePolicy(buttonSizePolicy);
100
101 m_moveDownButton = new KPushButton(i18n("Move Down"), buttonBox);
102 connect(m_moveDownButton, SIGNAL(clicked()),
103 this, SLOT(slotMoveDownButtonClicked()));
104 m_moveDownButton->setSizePolicy(buttonSizePolicy);
105
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);
109
110 topLayout->addWidget(hBox);
111
112 // insert all editable bookmarks.
113 KBookmarkGroup root = DolphinSettings::instance().bookmarkManager()->root();
114 KBookmark bookmark = root.first();
115
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());
122
123 // add hidden column to be able to retrieve the icon name again
124 item->setText(IconIdx, bookmark.icon());
125
126 m_listView->insertItem(item);
127 if (prev != 0) {
128 item->moveItem(prev);
129 }
130 prev = item;
131
132 bookmark = root.next(bookmark);
133 }
134 m_listView->setSelected(m_listView->firstChild(), true);
135
136 updateButtons();
137 }
138
139
140 BookmarksSettingsPage::~BookmarksSettingsPage()
141 {
142 }
143
144 void BookmarksSettingsPage::applySettings()
145 {
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();
153 }
154
155 // add all items as bookmarks
156 Q3ListViewItem* item = m_listView->firstChild();
157 while (item != 0) {
158 root.addBookmark(manager,
159 item->text(NameIdx),
160 KUrl(item->text(UrlIdx)),
161 item->text(IconIdx)); // hidden column
162 item = item->itemBelow();
163 }
164
165 manager->emitChanged(root);
166 }
167
168 void BookmarksSettingsPage::updateButtons()
169 {
170 const Q3ListViewItem* selectedItem = m_listView->selectedItem();
171 const bool hasSelection = (selectedItem != 0);
172
173 m_editButton->setEnabled(hasSelection);
174 m_removeButton->setEnabled(hasSelection);
175
176 const bool enableMoveUp = hasSelection &&
177 (selectedItem != m_listView->firstChild());
178 m_moveUpButton->setEnabled(enableMoveUp);
179
180 const bool enableMoveDown = hasSelection &&
181 (selectedItem != m_listView->lastChild());
182 m_moveDownButton->setEnabled(enableMoveDown);
183 }
184
185 void BookmarksSettingsPage::slotBookmarkDoubleClicked(Q3ListViewItem*,
186 const QPoint&,
187 int)
188 {
189 slotEditButtonClicked();
190 }
191
192 void BookmarksSettingsPage::slotBookmarkPressed(Q3ListViewItem* item)
193 {
194 if (item == 0) {
195 m_listView->setSelected(m_listView->currentItem(), true);
196 }
197 }
198
199 void BookmarksSettingsPage::slotAddButtonClicked()
200 {
201 KBookmark bookmark = EditBookmarkDialog::getBookmark(i18n("Add Bookmark"),
202 i18n("New bookmark"),
203 KUrl(),
204 "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);
213
214 Q3ListViewItem* lastItem = m_listView->lastChild();
215 if (lastItem != 0) {
216 item->moveItem(lastItem);
217 }
218
219 m_listView->setSelected(item, true);
220 updateButtons();
221 }
222 }
223
224 void BookmarksSettingsPage::slotEditButtonClicked()
225 {
226 Q3ListViewItem* item = m_listView->selectedItem();
227 assert(item != 0); // 'edit' may not get invoked when having no items
228
229 KBookmark bookmark = EditBookmarkDialog::getBookmark(i18n("Edit Bookmark"),
230 item->text(NameIdx),
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());
238 }
239 }
240
241 void BookmarksSettingsPage::slotRemoveButtonClicked()
242 {
243 Q3ListViewItem* selectedItem = m_listView->selectedItem();
244 assert(selectedItem != 0);
245 Q3ListViewItem* nextItem = selectedItem->itemBelow();
246 if (nextItem == 0) {
247 nextItem = selectedItem->itemAbove();
248 }
249
250 m_listView->takeItem(selectedItem);
251 if (nextItem != 0) {
252 m_listView->setSelected(nextItem, true);
253 }
254 }
255
256 void BookmarksSettingsPage::slotMoveUpButtonClicked()
257 {
258 moveBookmark(-1);
259 }
260
261 void BookmarksSettingsPage::slotMoveDownButtonClicked()
262 {
263 moveBookmark(+1);
264 }
265
266 int BookmarksSettingsPage::selectedBookmarkIndex() const
267 {
268 int index = -1;
269
270 Q3ListViewItem* selectedItem = m_listView->selectedItem();
271 if (selectedItem != 0) {
272 index = 0;
273 Q3ListViewItem* item = m_listView->firstChild();
274 while (item != selectedItem) {
275 item = item->nextSibling();
276 ++index;
277 }
278 }
279
280 return index;
281 }
282
283 void BookmarksSettingsPage::moveBookmark(int direction)
284 {
285 // this implementation currently only allows moving of bookmarks
286 // one step up or down
287 assert((direction >= -1) && (direction <= +1));
288
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();
294 assert(item != 0);
295
296 QPixmap pixmap;
297 if (item->pixmap(0) != 0) {
298 pixmap = *(item->pixmap(0));
299 }
300 QString name(item->text(NameIdx));
301 QString url(item->text(UrlIdx));
302 QString icon(item->text(IconIdx));
303
304 if (selectedItem->pixmap(0) != 0) {
305 item->setPixmap(PixmapIdx, *(selectedItem->pixmap(0)));
306 }
307 item->setText(NameIdx, selectedItem->text(NameIdx));
308 item->setText(UrlIdx, selectedItem->text(UrlIdx));
309 item->setText(IconIdx, selectedItem->text(IconIdx));
310
311 selectedItem->setPixmap(PixmapIdx, pixmap);
312 selectedItem->setText(NameIdx, name);
313 selectedItem->setText(UrlIdx, url);
314 selectedItem->setText(IconIdx, icon);
315
316 m_listView->setSelected(item, true);
317 }
318
319 #include "bookmarkssettingspage.moc"