]> cloud.milkyroute.net Git - dolphin.git/blob - src/bookmarkssettingspage.cpp
Allow zooming in and zooming out in the icons view.
[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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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(DolphinMainWindow* mainWindow,
46 QWidget*parent) :
47 SettingsPageBase(parent),
48 m_mainWindow(mainWindow),
49 m_addButton(0),
50 m_removeButton(0),
51 m_moveUpButton(0),
52 m_moveDownButton(0)
53 {
54 Q3VBoxLayout* topLayout = new Q3VBoxLayout(this, 2, KDialog::spacingHint());
55
56 const int spacing = KDialog::spacingHint();
57
58 KHBox* hBox = new KHBox(this);
59 hBox->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
60 hBox->setSpacing(spacing);
61 hBox->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Ignored);
62
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)));
77
78 KVBox* buttonBox = new KVBox(hBox);
79 buttonBox->setSpacing(spacing);
80
81 const QSizePolicy buttonSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
82
83 m_addButton = new KPushButton(i18n("Add..."), buttonBox);
84 connect(m_addButton, SIGNAL(clicked()),
85 this, SLOT(slotAddButtonClicked()));
86 m_addButton->setSizePolicy(buttonSizePolicy);
87
88 m_editButton = new KPushButton(i18n("Edit..."), buttonBox);
89 connect(m_editButton, SIGNAL(clicked()),
90 this, SLOT(slotEditButtonClicked()));
91 m_editButton->setSizePolicy(buttonSizePolicy);
92
93 m_removeButton = new KPushButton(i18n("Remove"), buttonBox);
94 connect(m_removeButton, SIGNAL(clicked()),
95 this, SLOT(slotRemoveButtonClicked()));
96 m_removeButton->setSizePolicy(buttonSizePolicy);
97
98 m_moveUpButton = new KPushButton(i18n("Move Up"), buttonBox);
99 connect(m_moveUpButton, SIGNAL(clicked()),
100 this, SLOT(slotMoveUpButtonClicked()));
101 m_moveUpButton->setSizePolicy(buttonSizePolicy);
102
103 m_moveDownButton = new KPushButton(i18n("Move Down"), buttonBox);
104 connect(m_moveDownButton, SIGNAL(clicked()),
105 this, SLOT(slotMoveDownButtonClicked()));
106 m_moveDownButton->setSizePolicy(buttonSizePolicy);
107
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);
111
112 topLayout->addWidget(hBox);
113
114 // insert all editable bookmarks.
115 KBookmarkGroup root = DolphinSettings::instance().bookmarkManager()->root();
116 KBookmark bookmark = root.first();
117
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());
124
125 // add hidden column to be able to retrieve the icon name again
126 item->setText(IconIdx, bookmark.icon());
127
128 m_listView->insertItem(item);
129 if (prev != 0) {
130 item->moveItem(prev);
131 }
132 prev = item;
133
134 bookmark = root.next(bookmark);
135 }
136 m_listView->setSelected(m_listView->firstChild(), true);
137
138 updateButtons();
139 }
140
141
142 BookmarksSettingsPage::~BookmarksSettingsPage()
143 {
144 }
145
146 void BookmarksSettingsPage::applySettings()
147 {
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();
155 }
156
157 // add all items as bookmarks
158 Q3ListViewItem* item = m_listView->firstChild();
159 while (item != 0) {
160 root.addBookmark(manager,
161 item->text(NameIdx),
162 KUrl(item->text(UrlIdx)),
163 item->text(IconIdx)); // hidden column
164 item = item->itemBelow();
165 }
166
167 manager->emitChanged(root);
168 }
169
170 void BookmarksSettingsPage::updateButtons()
171 {
172 const Q3ListViewItem* selectedItem = m_listView->selectedItem();
173 const bool hasSelection = (selectedItem != 0);
174
175 m_editButton->setEnabled(hasSelection);
176 m_removeButton->setEnabled(hasSelection);
177
178 const bool enableMoveUp = hasSelection &&
179 (selectedItem != m_listView->firstChild());
180 m_moveUpButton->setEnabled(enableMoveUp);
181
182 const bool enableMoveDown = hasSelection &&
183 (selectedItem != m_listView->lastChild());
184 m_moveDownButton->setEnabled(enableMoveDown);
185 }
186
187 void BookmarksSettingsPage::slotBookmarkDoubleClicked(Q3ListViewItem*,
188 const QPoint&,
189 int)
190 {
191 slotEditButtonClicked();
192 }
193
194 void BookmarksSettingsPage::slotBookmarkPressed(Q3ListViewItem* item)
195 {
196 if (item == 0) {
197 m_listView->setSelected(m_listView->currentItem(), true);
198 }
199 }
200
201 void BookmarksSettingsPage::slotAddButtonClicked()
202 {
203 KBookmark bookmark = EditBookmarkDialog::getBookmark(i18n("Add Bookmark"),
204 i18n("New bookmark"),
205 KUrl(),
206 "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);
215
216 Q3ListViewItem* lastItem = m_listView->lastChild();
217 if (lastItem != 0) {
218 item->moveItem(lastItem);
219 }
220
221 m_listView->setSelected(item, true);
222 updateButtons();
223 }
224 }
225
226 void BookmarksSettingsPage::slotEditButtonClicked()
227 {
228 Q3ListViewItem* item = m_listView->selectedItem();
229 assert(item != 0); // 'edit' may not get invoked when having no items
230
231 KBookmark bookmark = EditBookmarkDialog::getBookmark(i18n("Edit Bookmark"),
232 item->text(NameIdx),
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());
240 }
241 }
242
243 void BookmarksSettingsPage::slotRemoveButtonClicked()
244 {
245 Q3ListViewItem* selectedItem = m_listView->selectedItem();
246 assert(selectedItem != 0);
247 Q3ListViewItem* nextItem = selectedItem->itemBelow();
248 if (nextItem == 0) {
249 nextItem = selectedItem->itemAbove();
250 }
251
252 m_listView->takeItem(selectedItem);
253 if (nextItem != 0) {
254 m_listView->setSelected(nextItem, true);
255 }
256 }
257
258 void BookmarksSettingsPage::slotMoveUpButtonClicked()
259 {
260 moveBookmark(-1);
261 }
262
263 void BookmarksSettingsPage::slotMoveDownButtonClicked()
264 {
265 moveBookmark(+1);
266 }
267
268 int BookmarksSettingsPage::selectedBookmarkIndex() const
269 {
270 int index = -1;
271
272 Q3ListViewItem* selectedItem = m_listView->selectedItem();
273 if (selectedItem != 0) {
274 index = 0;
275 Q3ListViewItem* item = m_listView->firstChild();
276 while (item != selectedItem) {
277 item = item->nextSibling();
278 ++index;
279 }
280 }
281
282 return index;
283 }
284
285 void BookmarksSettingsPage::moveBookmark(int direction)
286 {
287 // this implementation currently only allows moving of bookmarks
288 // one step up or down
289 assert((direction >= -1) && (direction <= +1));
290
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();
296 assert(item != 0);
297
298 QPixmap pixmap;
299 if (item->pixmap(0) != 0) {
300 pixmap = *(item->pixmap(0));
301 }
302 QString name(item->text(NameIdx));
303 QString url(item->text(UrlIdx));
304 QString icon(item->text(IconIdx));
305
306 if (selectedItem->pixmap(0) != 0) {
307 item->setPixmap(PixmapIdx, *(selectedItem->pixmap(0)));
308 }
309 item->setText(NameIdx, selectedItem->text(NameIdx));
310 item->setText(UrlIdx, selectedItem->text(UrlIdx));
311 item->setText(IconIdx, selectedItem->text(IconIdx));
312
313 selectedItem->setPixmap(PixmapIdx, pixmap);
314 selectedItem->setText(NameIdx, name);
315 selectedItem->setText(UrlIdx, url);
316 selectedItem->setText(IconIdx, icon);
317
318 m_listView->setSelected(item, true);
319 }
320
321 #include "bookmarkssettingspage.moc"