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 <QButtonGroup>
40 #include <QGridLayout>
43 #include <QRadioButton>
44 #include <QVBoxLayout>
46 ViewPropertiesDialog::ViewPropertiesDialog(DolphinView
* dolphinView
) :
49 m_dolphinView(dolphinView
),
57 m_applyToCurrentFolder(0),
58 m_applyToSubFolders(0),
61 Q_ASSERT(dolphinView
!= 0);
62 const bool useGlobalViewProps
= DolphinSettings::instance().generalSettings()->globalViewProps();
64 setCaption(i18n("View Properties"));
65 setButtons(KDialog::Ok
| KDialog::Cancel
| KDialog::Apply
);
67 const KUrl
& url
= dolphinView
->url();
68 m_viewProps
= new ViewProperties(url
);
69 m_viewProps
->setAutoSaveEnabled(false);
71 QWidget
* main
= new QWidget();
72 QVBoxLayout
* topLayout
= new QVBoxLayout();
74 // create 'Properties' group containing view mode, sorting, sort order and show hidden files
75 QWidget
* propsBox
= main
;
76 if (!useGlobalViewProps
) {
77 propsBox
= new QGroupBox(i18n("Properties"), main
);
80 QLabel
* viewModeLabel
= new QLabel(i18n("View mode:"), propsBox
);
81 m_viewMode
= new QComboBox(propsBox
);
82 m_viewMode
->addItem(SmallIcon("view-icon"), i18n("Icons"));
83 m_viewMode
->addItem(SmallIcon("fileview-text"), i18n("Details"));
84 const int index
= static_cast<int>(m_viewProps
->viewMode());
85 m_viewMode
->setCurrentIndex(index
);
87 QLabel
* sortingLabel
= new QLabel(i18n("Sorting:"), propsBox
);
88 m_sorting
= new QComboBox(propsBox
);
89 m_sorting
->addItem("By Name");
90 m_sorting
->addItem("By Size");
91 m_sorting
->addItem("By Date");
92 m_sorting
->addItem("By Permissions");
93 m_sorting
->addItem("By Owner");
94 m_sorting
->addItem("By Group");
95 m_sorting
->setCurrentIndex(m_viewProps
->sorting());
97 QLabel
* sortOrderLabel
= new QLabel(i18n("Sort order:"), propsBox
);
98 m_sortOrder
= new QComboBox(propsBox
);
99 m_sortOrder
->addItem(i18n("Ascending"));
100 m_sortOrder
->addItem(i18n("Descending"));
101 const int sortOrderIdx
= (m_viewProps
->sortOrder() == Qt::Ascending
) ? 0 : 1;
102 m_sortOrder
->setCurrentIndex(sortOrderIdx
);
104 QLabel
* additionalInfoLabel
= new QLabel(i18n("Additional information:"), propsBox
);
105 m_additionalInfo
= new QComboBox(propsBox
);
106 m_additionalInfo
->addItem(i18n("No Information"), KFileItemDelegate::NoInformation
);
107 m_additionalInfo
->addItem(i18n("Type"), KFileItemDelegate::FriendlyMimeType
);
108 m_additionalInfo
->addItem(i18n("Size"), KFileItemDelegate::Size
);
109 m_additionalInfo
->addItem(i18n("Date"), KFileItemDelegate::ModificationTime
);
110 const int addInfoIndex
= m_additionalInfo
->findData(m_viewProps
->additionalInfo());
111 m_additionalInfo
->setCurrentIndex(addInfoIndex
);
112 m_additionalInfo
->setEnabled(m_viewProps
->viewMode() == DolphinView::IconsView
);
114 m_showPreview
= new QCheckBox(i18n("Show preview"), propsBox
);
115 m_showPreview
->setChecked(m_viewProps
->showPreview());
117 m_showHiddenFiles
= new QCheckBox(i18n("Show hidden files"), propsBox
);
118 m_showHiddenFiles
->setChecked(m_viewProps
->showHiddenFiles());
120 QGridLayout
* propsBoxLayout
= new QGridLayout(propsBox
);
121 propsBoxLayout
->addWidget(viewModeLabel
, 0, 0);
122 propsBoxLayout
->addWidget(m_viewMode
, 0, 1);
123 propsBoxLayout
->addWidget(m_sorting
, 1, 1);
124 propsBoxLayout
->addWidget(sortingLabel
, 1, 0);
125 propsBoxLayout
->addWidget(m_sorting
, 1, 1);
126 propsBoxLayout
->addWidget(sortOrderLabel
, 2, 0);
127 propsBoxLayout
->addWidget(m_sortOrder
, 2, 1);
128 propsBoxLayout
->addWidget(additionalInfoLabel
, 3, 0);
129 propsBoxLayout
->addWidget(m_additionalInfo
, 3, 1);
130 propsBoxLayout
->addWidget(m_showPreview
, 4, 0);
131 propsBoxLayout
->addWidget(m_showHiddenFiles
, 5, 0);
133 topLayout
->addWidget(propsBox
);
135 connect(m_viewMode
, SIGNAL(activated(int)),
136 this, SLOT(slotViewModeChanged(int)));
137 connect(m_sorting
, SIGNAL(activated(int)),
138 this, SLOT(slotSortingChanged(int)));
139 connect(m_sortOrder
, SIGNAL(activated(int)),
140 this, SLOT(slotSortOrderChanged(int)));
141 connect(m_additionalInfo
, SIGNAL(activated(int)),
142 this, SLOT(slotAdditionalInfoChanged(int)));
143 connect(m_showPreview
, SIGNAL(clicked()),
144 this, SLOT(slotShowPreviewChanged()));
145 connect(m_showHiddenFiles
, SIGNAL(clicked()),
146 this, SLOT(slotShowHiddenFilesChanged()));
148 connect(this, SIGNAL(okClicked()), this, SLOT(slotOk()));
149 connect(this, SIGNAL(applyClicked()), this, SLOT(slotApply()));
151 // Only show the following settings if the view properties are remembered
152 // for each directory:
153 if (!useGlobalViewProps
) {
154 // create 'Apply view properties to:' group
155 QGroupBox
* applyBox
= new QGroupBox(i18n("Apply view properties to:"), main
);
157 m_applyToCurrentFolder
= new QRadioButton(i18n("Current folder"), applyBox
);
158 m_applyToCurrentFolder
->setChecked(true);
159 m_applyToSubFolders
= new QRadioButton(i18n("Current folder including all sub folders"), applyBox
);
160 m_applyToAllFolders
= new QRadioButton(i18n("All folders"),applyBox
);
162 QButtonGroup
* applyGroup
= new QButtonGroup(this);
163 applyGroup
->addButton(m_applyToCurrentFolder
);
164 applyGroup
->addButton(m_applyToSubFolders
);
165 applyGroup
->addButton(m_applyToAllFolders
);
167 QVBoxLayout
* applyBoxLayout
= new QVBoxLayout(applyBox
);
168 applyBoxLayout
->addWidget(m_applyToCurrentFolder
);
169 applyBoxLayout
->addWidget(m_applyToSubFolders
);
170 applyBoxLayout
->addWidget(m_applyToAllFolders
);
172 m_useAsDefault
= new QCheckBox(i18n("Use as default for new folders"), main
);
174 topLayout
->addWidget(applyBox
);
175 topLayout
->addWidget(m_useAsDefault
);
177 connect(m_applyToCurrentFolder
, SIGNAL(clicked()),
178 this, SLOT(markAsDirty()));
179 connect(m_applyToSubFolders
, SIGNAL(clicked()),
180 this, SLOT(markAsDirty()));
181 connect(m_applyToAllFolders
, SIGNAL(clicked()),
182 this, SLOT(markAsDirty()));
183 connect(m_useAsDefault
, SIGNAL(clicked()),
184 this, SLOT(markAsDirty()));
187 main
->setLayout(topLayout
);
191 ViewPropertiesDialog::~ViewPropertiesDialog()
198 void ViewPropertiesDialog::slotOk()
200 applyViewProperties();
204 void ViewPropertiesDialog::slotApply()
206 applyViewProperties();
209 void ViewPropertiesDialog::slotViewModeChanged(int index
)
211 Q_ASSERT((index
>= 0) && (index
<= 1));
212 m_viewProps
->setViewMode(static_cast<DolphinView::Mode
>(index
));
215 m_additionalInfo
->setEnabled(m_viewProps
->viewMode() == DolphinView::IconsView
);
218 void ViewPropertiesDialog::slotSortingChanged(int index
)
220 const DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(index
);
221 m_viewProps
->setSorting(sorting
);
225 void ViewPropertiesDialog::slotSortOrderChanged(int index
)
227 Qt::SortOrder sortOrder
= (index
== 0) ? Qt::Ascending
: Qt::Descending
;
228 m_viewProps
->setSortOrder(sortOrder
);
232 void ViewPropertiesDialog::slotAdditionalInfoChanged(int index
)
234 KFileItemDelegate::AdditionalInformation info
= KFileItemDelegate::NoInformation
;
236 case 1: info
= KFileItemDelegate::FriendlyMimeType
; break;
237 case 2: info
= KFileItemDelegate::Size
; break;
238 case 3: info
= KFileItemDelegate::ModificationTime
; break;
241 m_viewProps
->setAdditionalInfo(info
);
245 void ViewPropertiesDialog::slotShowPreviewChanged()
247 const bool show
= m_showPreview
->isChecked();
248 m_viewProps
->setShowPreview(show
);
252 void ViewPropertiesDialog::slotShowHiddenFilesChanged()
254 const bool show
= m_showHiddenFiles
->isChecked();
255 m_viewProps
->setShowHiddenFiles(show
);
259 void ViewPropertiesDialog::markAsDirty()
264 void ViewPropertiesDialog::applyViewProperties()
266 const bool applyToSubFolders
= m_isDirty
&&
267 (m_applyToSubFolders
!= 0) &&
268 m_applyToSubFolders
->isChecked();
269 if (applyToSubFolders
) {
270 const QString
text(i18n("The view properties of all sub folders will be changed. Do you want to continue?"));
271 if (KMessageBox::questionYesNo(this, text
) == KMessageBox::No
) {
275 ViewPropsProgressInfo
* info
= new ViewPropsProgressInfo(m_dolphinView
,
276 m_dolphinView
->url(),
278 info
->setWindowModality(Qt::NonModal
);
282 const bool applyToAllFolders
= m_isDirty
&&
283 (m_applyToAllFolders
!= 0) &&
284 m_applyToAllFolders
->isChecked();
285 if (applyToAllFolders
) {
286 const QString
text(i18n("The view properties of all folders will be changed. Do you want to continue?"));
287 if (KMessageBox::questionYesNo(this, text
) == KMessageBox::No
) {
291 // Updating the global view properties time stamp in the general settings makes
292 // all existing viewproperties invalid, as they have a smaller time stamp.
293 GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
294 settings
->setViewPropsTimestamp(QDateTime::currentDateTime());
296 // This is also a good chance to make a cleanup of all mirrored view properties:
297 const KUrl mirroredDir
= ViewProperties::mirroredDirectory();
298 KIO::NetAccess::del(mirroredDir
, this);
303 m_dolphinView
->setMode(m_viewProps
->viewMode());
304 m_dolphinView
->setSorting(m_viewProps
->sorting());
305 m_dolphinView
->setSortOrder(m_viewProps
->sortOrder());
306 m_dolphinView
->setAdditionalInfo(m_viewProps
->additionalInfo());
307 m_dolphinView
->setShowPreview(m_viewProps
->showPreview());
308 m_dolphinView
->setShowHiddenFiles(m_viewProps
->showHiddenFiles());
312 if (m_useAsDefault
->isChecked()) {
313 // For directories where no .directory file is available, the .directory
314 // file stored for the global view properties is used as fallback. To update
315 // this file we temporary turn on the global view properties mode.
316 GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
317 Q_ASSERT(!settings
->globalViewProps());
319 settings
->setGlobalViewProps(true);
320 ViewProperties
defaultProps(m_dolphinView
->url());
321 defaultProps
.setDirProperties(*m_viewProps
);
323 settings
->setGlobalViewProps(false);
327 #include "viewpropertiesdialog.moc"