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