]> cloud.milkyroute.net Git - dolphin.git/blob - src/viewpropertiesdialog.cpp
Cleanup of URL drop handling (simplified code, modifier keys work again). After furth...
[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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "viewpropertiesdialog.h"
22 #include "viewpropsprogressinfo.h"
23 #include "dolphinview.h"
24 #include "dolphinsettings.h"
25 #include "generalsettings.h"
26 #include "viewproperties.h"
27
28 #include <klocale.h>
29 #include <kiconloader.h>
30 #include <kmessagebox.h>
31 #include <assert.h>
32
33 #include <QCheckBox>
34 #include <QComboBox>
35 #include <QGridLayout>
36 #include <QGroupBox>
37 #include <QLabel>
38 #include <QRadioButton>
39 #include <QVBoxLayout>
40
41 ViewPropertiesDialog::ViewPropertiesDialog(DolphinView* dolphinView) :
42 KDialog(dolphinView),
43 m_isDirty(false),
44 m_dolphinView(dolphinView),
45 m_viewProps(0),
46 m_viewMode(0),
47 m_sorting(0),
48 m_sortOrder(0),
49 m_showPreview(0),
50 m_showHiddenFiles(0),
51 m_applyToSubFolders(0),
52 m_useAsDefault(0)
53 {
54 assert(dolphinView != 0);
55
56 setCaption(i18n("View Properties"));
57 setButtons(KDialog::Ok | KDialog::Cancel | KDialog::Apply);
58
59 const KUrl& url = dolphinView->url();
60 m_viewProps = new ViewProperties(url);
61 m_viewProps->setAutoSaveEnabled(false);
62
63 QWidget* main = new QWidget();
64 QVBoxLayout* topLayout = new QVBoxLayout();
65
66 // create 'Properties' group containing view mode, sorting, sort order and show hidden files
67 QGroupBox* propsBox = new QGroupBox(i18n("Properties"), main);
68
69 QLabel* viewModeLabel = new QLabel(i18n("View mode:"), propsBox);
70 m_viewMode = new QComboBox(propsBox);
71 m_viewMode->addItem(SmallIcon("view_icon"), i18n("Icons"));
72 m_viewMode->addItem(SmallIcon("view_text"), i18n("Details"));
73 const int index = static_cast<int>(m_viewProps->viewMode());
74 m_viewMode->setCurrentIndex(index);
75
76 QLabel* sortingLabel = new QLabel(i18n("Sorting:"), propsBox);
77 m_sorting = new QComboBox(propsBox);
78 m_sorting->addItem("By Name");
79 m_sorting->addItem("By Size");
80 m_sorting->addItem("By Date");
81 int sortingIdx = 0;
82 switch (m_viewProps->sorting()) {
83 case DolphinView::SortByName: sortingIdx = 0; break;
84 case DolphinView::SortBySize: sortingIdx = 1; break;
85 case DolphinView::SortByDate: sortingIdx = 2; break;
86 default: break;
87 }
88 m_sorting->setCurrentIndex(sortingIdx);
89
90 QLabel* sortOrderLabel = new QLabel(i18n("Sort order:"), propsBox);
91 m_sortOrder = new QComboBox(propsBox);
92 m_sortOrder->addItem(i18n("Ascending"));
93 m_sortOrder->addItem(i18n("Descending"));
94 const int sortOrderIdx = (m_viewProps->sortOrder() == Qt::Ascending) ? 0 : 1;
95 m_sortOrder->setCurrentIndex(sortOrderIdx);
96
97 m_showPreview = new QCheckBox(i18n("Show preview"), propsBox);
98 m_showPreview->setChecked(m_viewProps->showPreview());
99
100 m_showHiddenFiles = new QCheckBox(i18n("Show hidden files"), propsBox);
101 m_showHiddenFiles->setChecked(m_viewProps->showHiddenFiles());
102
103 QGridLayout* propsBoxLayout = new QGridLayout(propsBox);
104 propsBoxLayout->addWidget(viewModeLabel, 0, 0);
105 propsBoxLayout->addWidget(m_viewMode, 0, 1);
106 propsBoxLayout->addWidget(m_sorting, 1, 1);
107 propsBoxLayout->addWidget(sortingLabel, 1, 0);
108 propsBoxLayout->addWidget(m_sorting, 1, 1);
109 propsBoxLayout->addWidget(sortOrderLabel, 2, 0);
110 propsBoxLayout->addWidget(m_sortOrder, 2, 1);
111 propsBoxLayout->addWidget(m_showPreview, 3, 0);
112 propsBoxLayout->addWidget(m_showHiddenFiles, 4, 0);
113
114 topLayout->addWidget(propsBox);
115
116 connect(m_viewMode, SIGNAL(activated(int)),
117 this, SLOT(slotViewModeChanged(int)));
118 connect(m_sorting, SIGNAL(activated(int)),
119 this, SLOT(slotSortingChanged(int)));
120 connect(m_sortOrder, SIGNAL(activated(int)),
121 this, SLOT(slotSortOrderChanged(int)));
122 connect(m_showPreview, SIGNAL(clicked()),
123 this, SLOT(slotShowPreviewChanged()));
124 connect(m_showHiddenFiles, SIGNAL(clicked()),
125 this, SLOT(slotShowHiddenFilesChanged()));
126
127 connect(this, SIGNAL(okClicked()), this, SLOT(slotOk()));
128 connect(this, SIGNAL(applyClicked()), this, SLOT(slotApply()));
129
130 // Only show the following settings if the view properties are remembered
131 // for each directory:
132 if (!DolphinSettings::instance().generalSettings()->globalViewProps()) {
133 m_applyToSubFolders = new QCheckBox(i18n("Apply changes to all sub folders"), main);
134 m_useAsDefault = new QCheckBox(i18n("Use as default"), main);
135 topLayout->addWidget(m_applyToSubFolders);
136 topLayout->addWidget(m_useAsDefault);
137
138 connect(m_applyToSubFolders, SIGNAL(clicked()),
139 this, SLOT(markAsDirty()));
140 connect(m_useAsDefault, SIGNAL(clicked()),
141 this, SLOT(markAsDirty()));
142 }
143
144 main->setLayout(topLayout);
145 setMainWidget(main);
146 }
147
148 ViewPropertiesDialog::~ViewPropertiesDialog()
149 {
150 m_isDirty = false;
151 delete m_viewProps;
152 m_viewProps = 0;
153 }
154
155 void ViewPropertiesDialog::slotOk()
156 {
157 applyViewProperties();
158 accept();
159 }
160
161 void ViewPropertiesDialog::slotApply()
162 {
163 applyViewProperties();
164 }
165
166 void ViewPropertiesDialog::slotViewModeChanged(int index)
167 {
168 assert((index >= 0) && (index <= 2));
169 m_viewProps->setViewMode(static_cast<DolphinView::Mode>(index));
170 m_isDirty = true;
171 }
172
173 void ViewPropertiesDialog::slotSortingChanged(int index)
174 {
175 DolphinView::Sorting sorting = DolphinView::SortByName;
176 switch (index) {
177 case 1: sorting = DolphinView::SortBySize; break;
178 case 2: sorting = DolphinView::SortByDate; break;
179 default: break;
180 }
181 m_viewProps->setSorting(sorting);
182 m_isDirty = true;
183 }
184
185 void ViewPropertiesDialog::slotSortOrderChanged(int index)
186 {
187 Qt::SortOrder sortOrder = (index == 0) ? Qt::Ascending : Qt::Descending;
188 m_viewProps->setSortOrder(sortOrder);
189 m_isDirty = true;
190 }
191
192 void ViewPropertiesDialog::slotShowPreviewChanged()
193 {
194 const bool show = m_showPreview->isChecked();
195 m_viewProps->setShowPreview(show);
196 m_isDirty = true;
197 }
198
199 void ViewPropertiesDialog::slotShowHiddenFilesChanged()
200 {
201 const bool show = m_showHiddenFiles->isChecked();
202 m_viewProps->setShowHiddenFiles(show);
203 m_isDirty = true;
204 }
205
206 void ViewPropertiesDialog::markAsDirty()
207 {
208 m_isDirty = true;
209 }
210
211 void ViewPropertiesDialog::applyViewProperties()
212 {
213 const bool applyToSubFolders = m_isDirty &&
214 (m_applyToSubFolders != 0) &&
215 m_applyToSubFolders->isChecked();
216 if (applyToSubFolders) {
217 const QString text(i18n("The view properties of all sub folders will be changed. Do you want to continue?"));
218 if (KMessageBox::questionYesNo(this, text) == KMessageBox::No) {
219 return;
220 }
221
222 ViewPropsProgressInfo* info = new ViewPropsProgressInfo(m_dolphinView,
223 m_dolphinView->url(),
224 *m_viewProps);
225 info->setWindowModality(Qt::NonModal);
226 info->show();
227 }
228
229 m_viewProps->save();
230
231 m_dolphinView->setMode(m_viewProps->viewMode());
232 m_dolphinView->setSorting(m_viewProps->sorting());
233 m_dolphinView->setSortOrder(m_viewProps->sortOrder());
234 m_dolphinView->setShowPreview(m_viewProps->showPreview());
235 m_dolphinView->setShowHiddenFiles(m_viewProps->showHiddenFiles());
236
237 m_isDirty = false;
238
239 // TODO: handle m_useAsDefault setting
240 }
241
242 #include "viewpropertiesdialog.moc"