]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/viewmodes/generalviewsettingspage.cpp
generalviewsettingspage: Use qobject_cast instead of static_cast
[dolphin.git] / src / settings / viewmodes / generalviewsettingspage.cpp
1 /*
2 * SPDX-FileCopyrightText: 2006 Peter Penz (peter.penz@gmx.at) and Patrice Tremblay
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "generalviewsettingspage.h"
8 #include "dolphin_generalsettings.h"
9 #include "dolphindebug.h"
10 #include "dolphinmainwindow.h"
11 #include "views/viewproperties.h"
12
13 #include <KActionCollection>
14 #include <KLocalizedString>
15
16 #include <QApplication>
17 #include <QButtonGroup>
18 #include <QCheckBox>
19 #include <QComboBox>
20 #include <QFontDatabase>
21 #include <QFormLayout>
22 #include <QLabel>
23 #include <QLineEdit>
24 #include <QMimeDatabase>
25 #include <QVBoxLayout>
26
27 GeneralViewSettingsPage::GeneralViewSettingsPage(const QUrl &url, QWidget *parent)
28 : SettingsPageBase(parent)
29 , m_url(url)
30 {
31 QFormLayout *topLayout = new QFormLayout(this);
32
33 // Display style
34 m_globalViewProps = new QRadioButton(i18nc("@option:radio", "Use common display style for all folders"));
35 // i18n: The information in this sentence contradicts the preceding sentence. That's what the word "still" is communicating.
36 // The previous sentence is "Use common display style for all folders".
37 QLabel *globalViewPropsLabel = new QLabel(i18nc("@info", "Some special views like search, recent files, or trash will still use a custom display style."));
38 globalViewPropsLabel->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));
39 globalViewPropsLabel->setWordWrap(true);
40 globalViewPropsLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
41
42 m_localViewProps = new QRadioButton(i18nc("@option:radio", "Remember display style for each folder"));
43 QLabel *localViewPropsLabel = new QLabel(i18nc("@info",
44 "Dolphin will add file system metadata to folders you change view properties for. If that is not possible, "
45 "a hidden .directory file is created instead."));
46 localViewPropsLabel->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));
47 localViewPropsLabel->setWordWrap(true);
48 localViewPropsLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
49
50 QButtonGroup *viewGroup = new QButtonGroup(this);
51 viewGroup->addButton(m_globalViewProps);
52 viewGroup->addButton(m_localViewProps);
53 topLayout->addRow(i18nc("@title:group", "Display style: "), m_globalViewProps);
54 topLayout->addRow(QString(), globalViewPropsLabel);
55 topLayout->addRow(QString(), m_localViewProps);
56 topLayout->addRow(QString(), localViewPropsLabel);
57
58 topLayout->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT, QSizePolicy::Fixed, QSizePolicy::Fixed));
59
60 // Browsing
61 m_openArchivesAsFolder = new QCheckBox(i18nc("@option:check", "Open archives as folder"));
62 m_autoExpandFolders = new QCheckBox(i18nc("option:check", "Open folders during drag operations"));
63 topLayout->addRow(i18nc("@title:group", "Browsing: "), m_openArchivesAsFolder);
64 topLayout->addRow(QString(), m_autoExpandFolders);
65
66 topLayout->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT, QSizePolicy::Fixed, QSizePolicy::Fixed));
67
68 #if HAVE_BALOO
69 // 'Show tooltips'
70 m_showToolTips = new QCheckBox(i18nc("@option:check", "Show item information on hover"));
71 topLayout->addRow(i18nc("@title:group", "Miscellaneous: "), m_showToolTips);
72 #endif
73
74 // 'Show selection marker'
75 m_showSelectionToggle = new QCheckBox(i18nc("@option:check", "Show selection marker"));
76 #if HAVE_BALOO
77 topLayout->addRow(QString(), m_showSelectionToggle);
78 #else
79 topLayout->addRow(i18nc("@title:group", "Miscellaneous: "), m_showSelectionToggle);
80 #endif
81
82 // 'Inline renaming of items'
83 m_renameInline = new QCheckBox(i18nc("option:check", "Rename single items inline"));
84 m_renameInline->setToolTip(i18n("Renaming multiple items is always done with a dialog window."));
85 topLayout->addRow(QString(), m_renameInline);
86
87 m_hideXtrashFiles = new QCheckBox(i18nc("option:check", "Also hide backup files while hiding hidden files"));
88 QMimeDatabase db;
89 QMimeType mime = db.mimeTypeForName(QStringLiteral("application/x-trash"));
90 m_hideXtrashFiles->setToolTip(i18nc("@info:tooltip %1 are the file patterns for mimetype application/x-trash",
91 "Backup files are the files whose mime-type is application/x-trash, patterns: %1",
92 (mime.globPatterns().join(", "))));
93 topLayout->addRow(QString(), m_hideXtrashFiles);
94
95 // --------------------- //
96 // START double click view background
97
98 // list of actions allowed to be triggered by double click
99 // actions were selected based on their usefulness of being triggered with the mouse
100 QStringList allowedActions{"new_tab",
101 "file_new",
102 "show_places_panel",
103 "show_information_panel",
104 "show_folders_panel",
105 "show_terminal_panel",
106 "open_terminal",
107 "go_up",
108 "go_back",
109 "go_home",
110 "view_redisplay",
111 "split_view",
112 "edit_select_all",
113 "toggle_selection_mode",
114 "create_dir",
115 "show_preview",
116 "show_hidden_files",
117 "show_in_groups",
118 "view_properties"};
119
120 // create actions combo-box and add actions
121 m_doubleClickViewComboBox = new QComboBox();
122 m_doubleClickViewComboBox->setAccessibleDescription(i18nc("Accessible description for combobox with actions of double click view background setting",
123 "Action to trigger when double clicking view background"));
124 // i18n: Completes the sentence "Double-click triggers [Nothing]".
125 m_doubleClickViewComboBox->addItem(QIcon::fromTheme("empty"), i18nc("@item:inlistbox", "Nothing"), QStringLiteral("none"));
126 m_doubleClickViewComboBox->addItem(QIcon::fromTheme("list-add"), i18nc("@item:inlistbox", "Custom Command"), customCommand);
127 m_doubleClickViewComboBox->insertSeparator(2);
128
129 DolphinMainWindow *mainWindow = qobject_cast<DolphinMainWindow *>(QApplication::activeWindow());
130
131 if (mainWindow != nullptr) {
132 KActionCollection *actions = mainWindow->actionCollection();
133 // get the allowed actions from actionCollection and add them to the combobox
134 for (const QString &actionName : allowedActions) {
135 QAction *action = actions->action(actionName);
136 if (action == nullptr) {
137 qCWarning(DolphinDebug) << QStringLiteral("Double click view: action `%1` was not found").arg(actionName);
138 continue;
139 }
140
141 QString actionText = action->text();
142 // remove ampersand used to define the action's shortcut
143 actionText.remove(QLatin1Char('&'));
144 m_doubleClickViewComboBox->addItem(action->icon(), actionText, action->objectName());
145 }
146 }
147 // i18n: This sentence is incomplete because the user can choose an action that is triggered in a combobox that will appear directly after "triggers".
148 // (While using a left-to-right language it will be to the right of "triggers", in a right-to-left layout it will be to the left.)
149 // So please try to keep this translation in a way that it is a complete sentence when reading the content of the combobox as part of the sentence.
150 // There can be many possible actions in the combobox. The default is "Nothing". Other actions are "New Tab", "Create Folder", "Show Hidden Files", …
151 QLabel *doubleClickViewLabel{new QLabel(i18nc("@info", "Double-click triggers"))};
152 QHBoxLayout *doubleClickViewHLayout{new QHBoxLayout()};
153 QWidget *doubleClickViewWidget{new QWidget()};
154 doubleClickViewWidget->setLayout(doubleClickViewHLayout);
155 doubleClickViewHLayout->addWidget(doubleClickViewLabel);
156 doubleClickViewHLayout->setContentsMargins(0, 0, 0, 0);
157 doubleClickViewHLayout->addWidget(m_doubleClickViewComboBox);
158 topLayout->addRow(i18nc("@title:group", "Background: "), doubleClickViewWidget);
159
160 m_doubleClickViewCustomAction = new QLineEdit();
161 m_doubleClickViewCustomAction->setAccessibleDescription(
162 i18nc("Accessible description for custom command text field of double click view background setting",
163 "Enter custom command to trigger when double clicking view background"));
164 m_doubleClickViewCustomAction->setPlaceholderText(i18nc("@info:placeholder for terminal command", "Command…"));
165 topLayout->addRow(QString(), m_doubleClickViewCustomAction);
166
167 m_doubleClickViewCustomActionInfo = new QLabel(i18nc("@label",
168 "Use {path} to get the path of the current folder. "
169 "Example: dolphin {path}"));
170 m_doubleClickViewCustomActionInfo->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));
171 m_doubleClickViewCustomActionInfo->setWordWrap(true);
172 m_doubleClickViewCustomActionInfo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
173 m_doubleClickViewCustomActionInfo->hide();
174 m_doubleClickViewCustomActionInfo->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard
175 | Qt::LinksAccessibleByKeyboard); // for accessibility
176 topLayout->addRow(QString(), m_doubleClickViewCustomActionInfo);
177 // END double click view background
178 // --------------------- //
179
180 loadSettings();
181
182 connect(m_localViewProps, &QRadioButton::toggled, this, &GeneralViewSettingsPage::changed);
183 connect(m_globalViewProps, &QRadioButton::toggled, this, &GeneralViewSettingsPage::changed);
184
185 connect(m_openArchivesAsFolder, &QCheckBox::toggled, this, &GeneralViewSettingsPage::changed);
186 connect(m_autoExpandFolders, &QCheckBox::toggled, this, &GeneralViewSettingsPage::changed);
187 #if HAVE_BALOO
188 connect(m_showToolTips, &QCheckBox::toggled, this, &GeneralViewSettingsPage::changed);
189 #endif
190 connect(m_showSelectionToggle, &QCheckBox::toggled, this, &GeneralViewSettingsPage::changed);
191 connect(m_renameInline, &QCheckBox::toggled, this, &GeneralViewSettingsPage::changed);
192 connect(m_hideXtrashFiles, &QCheckBox::toggled, this, &GeneralViewSettingsPage::changed);
193 connect(m_doubleClickViewCustomAction, &QLineEdit::textChanged, this, &GeneralViewSettingsPage::changed);
194 connect(m_doubleClickViewComboBox, qOverload<int>(&QComboBox::currentIndexChanged), this, &GeneralViewSettingsPage::changed);
195 connect(m_doubleClickViewComboBox, qOverload<int>(&QComboBox::currentIndexChanged), this, &GeneralViewSettingsPage::updateCustomActionVisibility);
196 }
197
198 GeneralViewSettingsPage::~GeneralViewSettingsPage()
199 {
200 }
201
202 void GeneralViewSettingsPage::applySettings()
203 {
204 GeneralSettings *settings = GeneralSettings::self();
205 ViewProperties props(m_url); // read current view properties
206 const bool useGlobalViewProps = m_globalViewProps->isChecked();
207 settings->setGlobalViewProps(useGlobalViewProps);
208 #if HAVE_BALOO
209 settings->setShowToolTips(m_showToolTips->isChecked());
210 #endif
211 settings->setShowSelectionToggle(m_showSelectionToggle->isChecked());
212 settings->setRenameInline(m_renameInline->isChecked());
213 settings->setHideXTrashFile(m_hideXtrashFiles->isChecked());
214 settings->setAutoExpandFolders(m_autoExpandFolders->isChecked());
215 settings->setBrowseThroughArchives(m_openArchivesAsFolder->isChecked());
216 settings->setDoubleClickViewCustomAction(m_doubleClickViewCustomAction->text());
217 settings->setDoubleClickViewAction(m_doubleClickViewComboBox->currentData().toString());
218 settings->save();
219 if (useGlobalViewProps) {
220 // Remember the global view properties by applying the current view properties.
221 // It is important that GeneralSettings::globalViewProps() is set before
222 // the class ViewProperties is used, as ViewProperties uses this setting
223 // to find the destination folder for storing the view properties.
224 ViewProperties globalProps(m_url);
225 globalProps.setDirProperties(props);
226 }
227 }
228
229 void GeneralViewSettingsPage::restoreDefaults()
230 {
231 GeneralSettings *settings = GeneralSettings::self();
232 settings->useDefaults(true);
233 loadSettings();
234 settings->useDefaults(false);
235 }
236
237 void GeneralViewSettingsPage::loadSettings()
238 {
239 const bool useGlobalViewProps = GeneralSettings::globalViewProps();
240 m_openArchivesAsFolder->setChecked(GeneralSettings::browseThroughArchives());
241 m_autoExpandFolders->setChecked(GeneralSettings::autoExpandFolders());
242 #if HAVE_BALOO
243 m_showToolTips->setChecked(GeneralSettings::showToolTips());
244 #endif
245 m_showSelectionToggle->setChecked(GeneralSettings::showSelectionToggle());
246 m_renameInline->setChecked(GeneralSettings::renameInline());
247 m_hideXtrashFiles->setChecked(GeneralSettings::hideXTrashFile());
248
249 m_localViewProps->setChecked(!useGlobalViewProps);
250 m_globalViewProps->setChecked(useGlobalViewProps);
251 int index = m_doubleClickViewComboBox->findData(GeneralSettings::doubleClickViewAction());
252 m_doubleClickViewComboBox->setCurrentIndex((index == -1) ? 0 : index);
253 m_doubleClickViewCustomAction->setText(GeneralSettings::doubleClickViewCustomAction());
254 updateCustomActionVisibility(m_doubleClickViewComboBox->currentIndex());
255 }
256
257 void GeneralViewSettingsPage::updateCustomActionVisibility(int doubleClickViewComboBoxCurrentIndex)
258 {
259 auto data = m_doubleClickViewComboBox->itemData(doubleClickViewComboBoxCurrentIndex, Qt::UserRole);
260 m_doubleClickViewCustomAction->setVisible(data == customCommand);
261 m_doubleClickViewCustomActionInfo->setVisible(data == customCommand);
262 }
263
264 #include "moc_generalviewsettingspage.cpp"