]> cloud.milkyroute.net Git - dolphin.git/blob - src/viewpropertiesdialog.cpp
commited initial version of Dolphin
[dolphin.git] / src / viewpropertiesdialog.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 "viewpropertiesdialog.h"
22 #include <klocale.h>
23 #include <qlabel.h>
24 #include <qlayout.h>
25 #include <q3vbox.h>
26 #include <q3buttongroup.h>
27 #include <qcheckbox.h>
28 #include <qradiobutton.h>
29 #include <qpushbutton.h>
30 #include <qsizepolicy.h>
31 #include <q3groupbox.h>
32 #include <qcombobox.h>
33 //Added by qt3to4:
34 #include <Q3VBoxLayout>
35 #include <kiconloader.h>
36 #include <kmessagebox.h>
37 #include <assert.h>
38
39 #include "viewproperties.h"
40 #include "dolphinview.h"
41
42 ViewPropertiesDialog::ViewPropertiesDialog(DolphinView* dolphinView) :
43 KDialog(dolphinView, static_cast<Qt::WFlags>(Ok /* KDE4-TODO: Apply | Cancel*/)),
44 m_isDirty(false),
45 m_dolphinView(dolphinView)
46 {
47 assert(dolphinView != 0);
48
49 const int margin = KDialog::marginHint();
50 const QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
51
52 const KUrl& url = dolphinView->url();
53 m_viewProps = new ViewProperties(url);
54 m_viewProps->setAutoSaveEnabled(false);
55
56 Q3VBoxLayout* topLayout = new Q3VBoxLayout(mainWidget(), 0, spacingHint());
57
58 // create 'Properties' group containing view mode, sorting, sort order and show hidden files
59 Q3GroupBox* propsGroup = new Q3GroupBox(2, Qt::Horizontal, i18n("Properties"), mainWidget());
60 propsGroup->setSizePolicy(sizePolicy);
61 propsGroup->setMargin(margin);
62
63 new QLabel(i18n("View mode:"), propsGroup);
64 m_viewMode = new QComboBox(propsGroup);
65 m_viewMode->insertItem(SmallIcon("view_icon"), i18n("Icons"));
66 m_viewMode->insertItem(SmallIcon("view_text"), i18n("Details"));
67 m_viewMode->insertItem(SmallIcon("gvdirpart"), i18n("Previews"));
68 const int index = static_cast<int>(m_viewProps->viewMode());
69 m_viewMode->setCurrentItem(index);
70
71 new QLabel(i18n("Sorting:"), propsGroup);
72 m_sorting = new QComboBox(propsGroup);
73 m_sorting->insertItem("By Name");
74 m_sorting->insertItem("By Size");
75 m_sorting->insertItem("By Date");
76 int sortingIdx = 0;
77 switch (m_viewProps->sorting()) {
78 case DolphinView::SortByName: sortingIdx = 0; break;
79 case DolphinView::SortBySize: sortingIdx = 1; break;
80 case DolphinView::SortByDate: sortingIdx = 2; break;
81 default: break;
82 }
83 m_sorting->setCurrentItem(sortingIdx);
84
85 new QLabel(i18n("Sort order:"), propsGroup);
86 m_sortOrder = new QComboBox(propsGroup);
87 m_sortOrder->insertItem(i18n("Ascending"));
88 m_sortOrder->insertItem(i18n("Descending"));
89 const int sortOrderIdx = (m_viewProps->sortOrder() == Qt::Ascending) ? 0 : 1;
90 m_sortOrder->setCurrentItem(sortOrderIdx);
91
92 m_showHiddenFiles = new QCheckBox(i18n("Show hidden files"), propsGroup);
93 m_showHiddenFiles->setChecked(m_viewProps->isShowHiddenFilesEnabled());
94
95 // create 'Apply view properties to:' group
96 Q3ButtonGroup* buttonGroup = new Q3ButtonGroup(3,
97 Qt::Vertical,
98 i18n("Apply view properties to:"),
99 mainWidget());
100 buttonGroup->setSizePolicy(sizePolicy);
101 buttonGroup->setMargin(margin);
102
103 m_applyToCurrentFolder = new QRadioButton(i18n("Current folder"), buttonGroup);
104 buttonGroup->insert(m_applyToCurrentFolder);
105
106 m_applyToSubFolders = new QRadioButton(i18n("Current folder including all sub folders"), buttonGroup);
107 buttonGroup->insert(m_applyToSubFolders);
108
109 m_applyToAllFolders = new QRadioButton(i18n("All folders"), buttonGroup);
110 buttonGroup->insert(m_applyToAllFolders);
111
112 if (m_viewProps->isValidForSubDirs()) {
113 m_applyToSubFolders->setChecked(true);
114 }
115 else {
116 m_applyToCurrentFolder->setChecked(true);
117 }
118
119 topLayout->addWidget(propsGroup);
120 topLayout->addWidget(buttonGroup);
121
122 connect(m_viewMode, SIGNAL(activated(int)),
123 this, SLOT(slotViewModeChanged(int)));
124 connect(m_sorting, SIGNAL(activated(int)),
125 this, SLOT(slotSortingChanged(int)));
126 connect(m_sortOrder, SIGNAL(activated(int)),
127 this, SLOT(slotSortOrderChanged(int)));
128 connect(m_showHiddenFiles, SIGNAL(clicked()),
129 this, SLOT(slotShowHiddenFilesChanged()));
130 connect(m_applyToCurrentFolder, SIGNAL(clicked()),
131 this, SLOT(slotApplyToCurrentFolder()));
132 connect(m_applyToSubFolders, SIGNAL(clicked()),
133 this, SLOT(slotApplyToSubFolders()));
134 connect(m_applyToAllFolders, SIGNAL(clicked()),
135 this, SLOT(slotApplyToAllFolders()));
136 }
137
138 ViewPropertiesDialog::~ViewPropertiesDialog()
139 {
140 m_isDirty = false;
141 delete m_viewProps;
142 m_viewProps = 0;
143 }
144
145 void ViewPropertiesDialog::slotOk()
146 {
147 applyViewProperties();
148 // KDE4-TODO: KDialog::slotOk();
149 }
150
151 void ViewPropertiesDialog::slotApply()
152 {
153 applyViewProperties();
154 // KDE4-TODO: KDialog::slotApply();
155 }
156
157 void ViewPropertiesDialog::slotViewModeChanged(int index)
158 {
159 assert((index >= 0) && (index <= 2));
160 m_viewProps->setViewMode(static_cast<DolphinView::Mode>(index));
161 m_isDirty = true;
162 }
163
164 void ViewPropertiesDialog::slotSortingChanged(int index)
165 {
166 DolphinView::Sorting sorting = DolphinView::SortByName;
167 switch (index) {
168 case 1: sorting = DolphinView::SortBySize; break;
169 case 2: sorting = DolphinView::SortByDate; break;
170 default: break;
171 }
172 m_viewProps->setSorting(sorting);
173 m_isDirty = true;
174 }
175
176 void ViewPropertiesDialog::slotSortOrderChanged(int index)
177 {
178 Qt::SortOrder sortOrder = (index == 0) ? Qt::Ascending : Qt::Descending;
179 m_viewProps->setSortOrder(sortOrder);
180 m_isDirty = true;
181 }
182
183 void ViewPropertiesDialog::slotShowHiddenFilesChanged()
184 {
185 const bool show = m_showHiddenFiles->isChecked();
186 m_viewProps->setShowHiddenFilesEnabled(show);
187 m_isDirty = true;
188 }
189
190 void ViewPropertiesDialog::slotApplyToCurrentFolder()
191 {
192 m_viewProps->setValidForSubDirs(false);
193 m_isDirty = true;
194 }
195
196 void ViewPropertiesDialog::slotApplyToSubFolders()
197 {
198 m_viewProps->setValidForSubDirs(true);
199 m_isDirty = true;
200 }
201
202 void ViewPropertiesDialog::slotApplyToAllFolders()
203 {
204 m_isDirty = true;
205 }
206
207 void ViewPropertiesDialog::applyViewProperties()
208 {
209 if (m_applyToAllFolders->isChecked()) {
210 if (m_isDirty) {
211 const QString text(i18n("The view properties of all folders will be replaced. Do you want to continue?"));
212 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
213 return;
214 }
215 }
216
217 ViewProperties props(QDir::homeDirPath());
218 props.setViewMode(m_viewProps->viewMode());
219 props.setSorting(m_viewProps->sorting());
220 props.setSortOrder(m_viewProps->sortOrder());
221 props.setShowHiddenFilesEnabled(m_viewProps->isShowHiddenFilesEnabled());
222 props.setValidForSubDirs(true);
223 }
224 else if (m_applyToSubFolders->isChecked() && m_isDirty) {
225 const QString text(i18n("The view properties of all sub folders will be replaced. Do you want to continue?"));
226 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
227 return;
228 }
229 }
230
231 m_viewProps->save();
232 m_dolphinView->setViewProperties(*m_viewProps);
233 m_isDirty = false;
234 }
235
236 #include "viewpropertiesdialog.moc"