]> cloud.milkyroute.net Git - dolphin.git/blob - src/sidebar.cpp
Make it (almost) possible to have more than one Dolphin KMainWindow
[dolphin.git] / src / sidebar.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Cvetoslav Ludmiloff <ludmiloff@gmail.com> *
3 * *
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. *
8 * *
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. *
13 * *
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
19
20 #include "sidebar.h"
21
22 #include <QVBoxLayout>
23 #include <QComboBox>
24
25 #include <kiconloader.h>
26 #include <klocale.h>
27
28 #include "dolphinsettings.h"
29 #include "sidebarsettings.h"
30 #include "bookmarkssidebarpage.h"
31 #include "infosidebarpage.h"
32
33 Sidebar::Sidebar(DolphinMainWindow* mainWindow, QWidget* parent) :
34 QWidget(parent),
35 m_mainWindow(mainWindow),
36 m_pagesSelector(0),
37 m_page(0),
38 m_layout(0)
39 {
40 m_layout = new QVBoxLayout(this);
41 m_layout->setMargin(0);
42 m_layout->setSpacing(0);
43
44 m_pagesSelector = new QComboBox(this);
45 m_pagesSelector->insertItem(i18n("Information"));
46 m_pagesSelector->insertItem(i18n("Bookmarks"));
47
48 // Assure that the combo box has the same height as the Url navigator for
49 // a clean layout.
50 // TODO: the following 2 lines have been copied from the UrlNavigator
51 // constructor (-> provide a shared height setting?)
52 QFontMetrics fontMetrics(font());
53 m_pagesSelector->setMinimumHeight(fontMetrics.height() + 8);
54
55 SidebarSettings* settings = DolphinSettings::instance().sidebarSettings();
56 const int selectedIndex = indexForName(settings->selectedPage());
57 m_pagesSelector->setCurrentItem(selectedIndex);
58 m_layout->addWidget(m_pagesSelector);
59
60 createPage(selectedIndex);
61
62 connect(m_pagesSelector, SIGNAL(activated(int)),
63 this, SLOT(createPage(int)));
64 }
65
66 Sidebar::~Sidebar()
67 {
68 }
69
70 QSize Sidebar::sizeHint() const
71 {
72 QSize size(QWidget::sizeHint());
73
74 SidebarSettings* settings = DolphinSettings::instance().sidebarSettings();
75 size.setWidth(settings->width());
76 return size;
77 }
78
79 void Sidebar::createPage(int index)
80 {
81 if (m_page != 0) {
82 m_page->deleteLater();
83 m_page = 0;
84 }
85
86 switch (index) {
87 case 0: m_page = new InfoSidebarPage(m_mainWindow, this); break;
88 case 1: m_page = new BookmarksSidebarPage(m_mainWindow, this); break;
89 default: break;
90 }
91
92 m_layout->addWidget(m_page);
93 m_page->show();
94
95 SidebarSettings* settings = DolphinSettings::instance().sidebarSettings();
96 settings->setSelectedPage(m_pagesSelector->text(index));
97 }
98
99 int Sidebar::indexForName(const QString& name) const
100 {
101 const int count = m_pagesSelector->count();
102 for (int i = 0; i < count; ++i) {
103 if (m_pagesSelector->text(i) == name) {
104 return i;
105 }
106 }
107
108 return 0;
109 }
110
111 #include "sidebar.moc"