1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz *
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. *
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. *
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 ***************************************************************************/
21 #include "viewpropertiesdialog.h"
22 #include "viewpropsprogressinfo.h"
23 #include "dolphinview.h"
24 #include "dolphinsettings.h"
25 #include "dolphinsortfilterproxymodel.h"
26 #include "dolphin_generalsettings.h"
27 #include "viewproperties.h"
29 #include <kcomponentdata.h>
31 #include <kiconloader.h>
32 #include <kio/netaccess.h>
33 #include <kmessagebox.h>
34 #include <kstandarddirs.h>
37 #include <QtGui/QButtonGroup>
38 #include <QtGui/QCheckBox>
39 #include <QtGui/QComboBox>
40 #include <QtGui/QGridLayout>
41 #include <QtGui/QGroupBox>
42 #include <QtGui/QLabel>
43 #include <QtGui/QRadioButton>
44 #include <QtGui/QBoxLayout>
46 ViewPropertiesDialog::ViewPropertiesDialog(DolphinView
* dolphinView
) :
49 m_dolphinView(dolphinView
),
54 m_categorizedSorting(0),
58 m_applyToCurrentFolder(0),
59 m_applyToSubFolders(0),
62 Q_ASSERT(dolphinView
!= 0);
63 const bool useGlobalViewProps
= DolphinSettings::instance().generalSettings()->globalViewProps();
65 setCaption(i18n("View Properties"));
66 setButtons(KDialog::Ok
| KDialog::Cancel
| KDialog::Apply
);
68 const KUrl
& url
= dolphinView
->url();
69 m_viewProps
= new ViewProperties(url
);
70 m_viewProps
->setAutoSaveEnabled(false);
72 QWidget
* main
= new QWidget();
73 QVBoxLayout
* topLayout
= new QVBoxLayout();
75 // create 'Properties' group containing view mode, sorting, sort order and show hidden files
76 QWidget
* propsBox
= main
;
77 if (!useGlobalViewProps
) {
78 propsBox
= new QGroupBox(i18n("Properties"), main
);
81 QLabel
* viewModeLabel
= new QLabel(i18n("View mode:"), propsBox
);
82 m_viewMode
= new QComboBox(propsBox
);
83 m_viewMode
->addItem(KIcon("view-icon"), i18n("Icons"));
84 m_viewMode
->addItem(KIcon("fileview-text"), i18n("Details"));
85 m_viewMode
->addItem(KIcon("fileview-column"), i18n("Column"));
86 const int index
= static_cast<int>(m_viewProps
->viewMode());
87 m_viewMode
->setCurrentIndex(index
);
88 const bool iconsViewEnabled
= (index
== DolphinView::IconsView
);
90 QLabel
* sortingLabel
= new QLabel(i18n("Sorting:"), propsBox
);
91 QWidget
* sortingBox
= new QWidget(propsBox
);
93 m_sortOrder
= new QComboBox(sortingBox
);
94 m_sortOrder
->addItem(i18n("Ascending"));
95 m_sortOrder
->addItem(i18n("Descending"));
96 const int sortOrderIdx
= (m_viewProps
->sortOrder() == Qt::AscendingOrder
) ? 0 : 1;
97 m_sortOrder
->setCurrentIndex(sortOrderIdx
);
99 m_categorizedSorting
= new QComboBox(sortingBox
);
100 m_categorizedSorting
->addItem(i18n("Ungrouped"));
101 m_categorizedSorting
->addItem(i18n("Show in Groups"));
102 m_categorizedSorting
->setCurrentIndex(m_viewProps
->categorizedSorting() ? 1 : 0);
103 m_categorizedSorting
->setEnabled(iconsViewEnabled
);
105 m_sorting
= new QComboBox(sortingBox
);
106 m_sorting
->addItem("By Name");
107 m_sorting
->addItem("By Size");
108 m_sorting
->addItem("By Date");
109 m_sorting
->addItem("By Permissions");
110 m_sorting
->addItem("By Owner");
111 m_sorting
->addItem("By Group");
112 m_sorting
->addItem("By Type");
113 m_sorting
->setCurrentIndex(m_viewProps
->sorting());
115 QHBoxLayout
* sortingLayout
= new QHBoxLayout();
116 sortingLayout
->setMargin(0);
117 sortingLayout
->addWidget(m_sortOrder
);
118 sortingLayout
->addWidget(m_sorting
);
119 sortingLayout
->addWidget(m_categorizedSorting
);
120 sortingBox
->setLayout(sortingLayout
);
122 QLabel
* additionalInfoLabel
= new QLabel(i18n("Additional information:"), propsBox
);
123 m_additionalInfo
= new QComboBox(propsBox
);
124 m_additionalInfo
->addItem(i18n("No Information"), KFileItemDelegate::NoInformation
);
125 m_additionalInfo
->addItem(i18n("Type"), KFileItemDelegate::FriendlyMimeType
);
126 m_additionalInfo
->addItem(i18n("Size"), KFileItemDelegate::Size
);
127 m_additionalInfo
->addItem(i18n("Date"), KFileItemDelegate::ModificationTime
);
128 const int addInfoIndex
= m_additionalInfo
->findData(m_viewProps
->additionalInfo());
129 m_additionalInfo
->setCurrentIndex(addInfoIndex
);
130 m_additionalInfo
->setEnabled(iconsViewEnabled
);
132 m_showPreview
= new QCheckBox(i18n("Show preview"), propsBox
);
133 m_showPreview
->setChecked(m_viewProps
->showPreview());
135 m_showHiddenFiles
= new QCheckBox(i18n("Show hidden files"), propsBox
);
136 m_showHiddenFiles
->setChecked(m_viewProps
->showHiddenFiles());
138 QGridLayout
* propsBoxLayout
= new QGridLayout(propsBox
);
139 propsBoxLayout
->addWidget(viewModeLabel
, 0, 0);
140 propsBoxLayout
->addWidget(m_viewMode
, 0, 1);
141 propsBoxLayout
->addWidget(sortingLabel
, 1, 0);
142 propsBoxLayout
->addWidget(sortingBox
, 1, 1);
143 propsBoxLayout
->addWidget(additionalInfoLabel
, 2, 0);
144 propsBoxLayout
->addWidget(m_additionalInfo
, 2, 1);
145 propsBoxLayout
->addWidget(m_showPreview
, 3, 0);
146 propsBoxLayout
->addWidget(m_showHiddenFiles
, 4, 0);
148 topLayout
->addWidget(propsBox
);
150 connect(m_viewMode
, SIGNAL(activated(int)),
151 this, SLOT(slotViewModeChanged(int)));
152 connect(m_sorting
, SIGNAL(activated(int)),
153 this, SLOT(slotSortingChanged(int)));
154 connect(m_sortOrder
, SIGNAL(activated(int)),
155 this, SLOT(slotSortOrderChanged(int)));
156 connect(m_categorizedSorting
, SIGNAL(activated(int)),
157 this, SLOT(slotCategorizedSortingChanged(int)));
158 connect(m_additionalInfo
, SIGNAL(activated(int)),
159 this, SLOT(slotAdditionalInfoChanged(int)));
160 connect(m_showPreview
, SIGNAL(clicked()),
161 this, SLOT(slotShowPreviewChanged()));
162 connect(m_showHiddenFiles
, SIGNAL(clicked()),
163 this, SLOT(slotShowHiddenFilesChanged()));
165 connect(this, SIGNAL(okClicked()), this, SLOT(slotOk()));
166 connect(this, SIGNAL(applyClicked()), this, SLOT(slotApply()));
168 // Only show the following settings if the view properties are remembered
169 // for each directory:
170 if (!useGlobalViewProps
) {
171 // create 'Apply View Properties To' group
172 QGroupBox
* applyBox
= new QGroupBox(i18n("Apply View Properties To"), main
);
174 m_applyToCurrentFolder
= new QRadioButton(i18n("Current folder"), applyBox
);
175 m_applyToCurrentFolder
->setChecked(true);
176 m_applyToSubFolders
= new QRadioButton(i18n("Current folder including all sub folders"), applyBox
);
177 m_applyToAllFolders
= new QRadioButton(i18n("All folders"), applyBox
);
179 QButtonGroup
* applyGroup
= new QButtonGroup(this);
180 applyGroup
->addButton(m_applyToCurrentFolder
);
181 applyGroup
->addButton(m_applyToSubFolders
);
182 applyGroup
->addButton(m_applyToAllFolders
);
184 QVBoxLayout
* applyBoxLayout
= new QVBoxLayout(applyBox
);
185 applyBoxLayout
->addWidget(m_applyToCurrentFolder
);
186 applyBoxLayout
->addWidget(m_applyToSubFolders
);
187 applyBoxLayout
->addWidget(m_applyToAllFolders
);
189 m_useAsDefault
= new QCheckBox(i18n("Use as default for new folders"), main
);
191 topLayout
->addWidget(applyBox
);
192 topLayout
->addWidget(m_useAsDefault
);
194 connect(m_applyToCurrentFolder
, SIGNAL(clicked()),
195 this, SLOT(markAsDirty()));
196 connect(m_applyToSubFolders
, SIGNAL(clicked()),
197 this, SLOT(markAsDirty()));
198 connect(m_applyToAllFolders
, SIGNAL(clicked()),
199 this, SLOT(markAsDirty()));
200 connect(m_useAsDefault
, SIGNAL(clicked()),
201 this, SLOT(markAsDirty()));
204 main
->setLayout(topLayout
);
208 ViewPropertiesDialog::~ViewPropertiesDialog()
215 void ViewPropertiesDialog::slotOk()
217 applyViewProperties();
221 void ViewPropertiesDialog::slotApply()
223 applyViewProperties();
226 void ViewPropertiesDialog::slotViewModeChanged(int index
)
228 m_viewProps
->setViewMode(static_cast<DolphinView::Mode
>(index
));
231 const bool iconsViewEnabled
= (m_viewProps
->viewMode() == DolphinView::IconsView
);
232 m_categorizedSorting
->setEnabled(iconsViewEnabled
);
233 m_additionalInfo
->setEnabled(iconsViewEnabled
);
236 void ViewPropertiesDialog::slotSortingChanged(int index
)
238 const DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(index
);
239 m_viewProps
->setSorting(sorting
);
243 void ViewPropertiesDialog::slotSortOrderChanged(int index
)
245 Qt::SortOrder sortOrder
= (index
== 0) ? Qt::AscendingOrder
: Qt::DescendingOrder
;
246 m_viewProps
->setSortOrder(sortOrder
);
250 void ViewPropertiesDialog::slotCategorizedSortingChanged(int index
)
252 m_viewProps
->setCategorizedSorting(index
== 1);
256 void ViewPropertiesDialog::slotAdditionalInfoChanged(int index
)
258 KFileItemDelegate::AdditionalInformation info
= KFileItemDelegate::NoInformation
;
260 case 1: info
= KFileItemDelegate::FriendlyMimeType
; break;
261 case 2: info
= KFileItemDelegate::Size
; break;
262 case 3: info
= KFileItemDelegate::ModificationTime
; break;
265 m_viewProps
->setAdditionalInfo(info
);
269 void ViewPropertiesDialog::slotShowPreviewChanged()
271 const bool show
= m_showPreview
->isChecked();
272 m_viewProps
->setShowPreview(show
);
276 void ViewPropertiesDialog::slotShowHiddenFilesChanged()
278 const bool show
= m_showHiddenFiles
->isChecked();
279 m_viewProps
->setShowHiddenFiles(show
);
283 void ViewPropertiesDialog::markAsDirty()
288 void ViewPropertiesDialog::applyViewProperties()
290 const bool applyToSubFolders
= m_isDirty
&&
291 (m_applyToSubFolders
!= 0) &&
292 m_applyToSubFolders
->isChecked();
293 if (applyToSubFolders
) {
294 const QString
text(i18n("The view properties of all sub folders will be changed. Do you want to continue?"));
295 if (KMessageBox::questionYesNo(this, text
) == KMessageBox::No
) {
299 ViewPropsProgressInfo
* info
= new ViewPropsProgressInfo(m_dolphinView
,
300 m_dolphinView
->url(),
302 info
->setWindowModality(Qt::NonModal
);
306 const bool applyToAllFolders
= m_isDirty
&&
307 (m_applyToAllFolders
!= 0) &&
308 m_applyToAllFolders
->isChecked();
309 if (applyToAllFolders
) {
310 const QString
text(i18n("The view properties of all folders will be changed. Do you want to continue?"));
311 if (KMessageBox::questionYesNo(this, text
) == KMessageBox::No
) {
315 // Updating the global view properties time stamp in the general settings makes
316 // all existing viewproperties invalid, as they have a smaller time stamp.
317 GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
318 settings
->setViewPropsTimestamp(QDateTime::currentDateTime());
320 // This is also a good chance to make a cleanup of all mirrored view properties:
321 const KUrl mirroredDir
= ViewProperties::mirroredDirectory();
322 KIO::NetAccess::del(mirroredDir
, this);
327 m_dolphinView
->setMode(m_viewProps
->viewMode());
328 m_dolphinView
->setSorting(m_viewProps
->sorting());
329 m_dolphinView
->setSortOrder(m_viewProps
->sortOrder());
330 m_dolphinView
->setCategorizedSorting(m_viewProps
->categorizedSorting());
331 m_dolphinView
->setAdditionalInfo(m_viewProps
->additionalInfo());
332 m_dolphinView
->setShowPreview(m_viewProps
->showPreview());
333 m_dolphinView
->setShowHiddenFiles(m_viewProps
->showHiddenFiles());
337 if (m_useAsDefault
->isChecked()) {
338 // For directories where no .directory file is available, the .directory
339 // file stored for the global view properties is used as fallback. To update
340 // this file we temporary turn on the global view properties mode.
341 GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
342 Q_ASSERT(!settings
->globalViewProps());
344 settings
->setGlobalViewProps(true);
345 ViewProperties
defaultProps(m_dolphinView
->url());
346 defaultProps
.setDirProperties(*m_viewProps
);
348 settings
->setGlobalViewProps(false);
352 #include "viewpropertiesdialog.moc"