1/*
2 SPDX-FileCopyrightText: 2011-2018 Dominik Haumann <dhaumann@kde.org>
3 SPDX-FileCopyrightText: 2013 Gerald Senarclens de Grancy <oss@senarclens.eu>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "variablelineedit.h"
9
10#include "kateautoindent.h"
11#include "katedocument.h"
12#include "kateglobal.h"
13#include "katesyntaxmanager.h"
14#include "kateview.h"
15#include "variableitem.h"
16#include "variablelistview.h"
17
18#include <QApplication>
19#include <QComboBox>
20#include <QFrame>
21#include <QHBoxLayout>
22#include <QLineEdit>
23#include <QSizeGrip>
24#include <QStyle>
25#include <QToolButton>
26#include <QVBoxLayout>
27
28#include <KLocalizedString>
29
30#include <sonnet/speller.h>
31
32VariableLineEdit::VariableLineEdit(QWidget *parent)
33 : QWidget(parent)
34{
35 QHBoxLayout *hl = new QHBoxLayout(this);
36 hl->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
37
38 m_lineedit = new QLineEdit(this);
39 m_button = new QToolButton(this);
40 m_button->setIcon(QIcon::fromTheme(QStringLiteral("tools-wizard")));
41 m_button->setToolTip(i18n("Show list of valid variables."));
42
43 hl->addWidget(m_lineedit);
44 hl->addWidget(m_button);
45
46 m_popup.reset(p: new QFrame(nullptr, Qt::Popup));
47 m_popup->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
48 QVBoxLayout *l = new QVBoxLayout(m_popup.get());
49 l->setSpacing(0);
50 l->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
51
52 // forward text changed signal
53 connect(sender: m_lineedit, signal: &QLineEdit::textChanged, context: this, slot: &VariableLineEdit::textChanged);
54
55 // open popup on button click
56 connect(sender: m_button, signal: &QToolButton::clicked, context: this, slot: &VariableLineEdit::editVariables);
57}
58
59void VariableLineEdit::editVariables()
60{
61 m_listview = new VariableListView(m_lineedit->text(), m_popup.get());
62 addKateItems(listview: m_listview);
63 connect(sender: m_listview, signal: &VariableListView::aboutToHide, context: this, slot: &VariableLineEdit::updateVariableLine);
64
65 m_popup->layout()->addWidget(w: m_listview);
66
67 if (layoutDirection() == Qt::LeftToRight) {
68 QPoint topLeft = mapToGlobal(m_lineedit->geometry().bottomLeft());
69 const int w = m_button->geometry().right() - m_lineedit->geometry().left();
70 const int h = 300; //(w * 2) / 4;
71 m_popup->setGeometry(QRect(topLeft, QSize(w, h)));
72 } else {
73 QPoint topLeft = mapToGlobal(m_button->geometry().bottomLeft());
74 const int w = m_lineedit->geometry().right() - m_button->geometry().left();
75 const int h = 300; //(w * 2) / 4;
76 m_popup->setGeometry(QRect(topLeft, QSize(w, h)));
77 }
78 m_popup->show();
79}
80
81void VariableLineEdit::updateVariableLine()
82{
83 Q_ASSERT(m_listview);
84
85 QString variables = m_listview->variableLine();
86 m_lineedit->setText(variables);
87
88 m_popup->layout()->removeWidget(w: m_listview);
89 m_listview->deleteLater();
90 m_listview = nullptr;
91}
92
93void VariableLineEdit::addKateItems(VariableListView *listview)
94{
95 VariableItem *item = nullptr;
96
97 // If a current active doc is available
98 KTextEditor::ViewPrivate *activeView = nullptr;
99 KTextEditor::DocumentPrivate *activeDoc = nullptr;
100
101 KateDocumentConfig *docConfig = KateDocumentConfig::global();
102 KateViewConfig *viewConfig = KateViewConfig::global();
103 KateRendererConfig *rendererConfig = KateRendererConfig::global();
104
105 if ((activeView = qobject_cast<KTextEditor::ViewPrivate *>(object: KTextEditor::EditorPrivate::self()->application()->activeMainWindow()->activeView()))) {
106 activeDoc = activeView->doc();
107 viewConfig = activeView->config();
108 docConfig = activeDoc->config();
109 rendererConfig = activeView->rendererConfig();
110 }
111
112 // Add 'auto-brackets' to list
113 item = new VariableBoolItem(QStringLiteral("auto-brackets"), false);
114 item->setHelpText(i18nc("short translation please", "Enable automatic insertion of brackets."));
115 listview->addItem(item);
116
117 // Add 'auto-center-lines' to list
118 item = new VariableIntItem(QStringLiteral("auto-center-lines"), viewConfig->autoCenterLines());
119 static_cast<VariableIntItem *>(item)->setRange(minValue: 1, maxValue: 100);
120 item->setHelpText(i18nc("short translation please", "Set the number of autocenter lines."));
121 listview->addItem(item);
122
123 // Add 'background-color' to list
124 item = new VariableColorItem(QStringLiteral("background-color"), rendererConfig->backgroundColor());
125 item->setHelpText(i18nc("short translation please", "Set the document background color."));
126 listview->addItem(item);
127
128 // Add 'backspace-indents' to list
129 item = new VariableBoolItem(QStringLiteral("backspace-indents"), docConfig->backspaceIndents());
130 item->setHelpText(i18nc("short translation please", "Pressing backspace in leading whitespace unindents."));
131 listview->addItem(item);
132
133 // Add 'block-selection' to list
134 item = new VariableBoolItem(QStringLiteral("block-selection"), false);
135 if (activeView) {
136 static_cast<VariableBoolItem *>(item)->setValue(activeView->blockSelection());
137 }
138 item->setHelpText(i18nc("short translation please", "Enable block selection mode."));
139 listview->addItem(item);
140
141 // Add 'byte-order-mark' (bom) to list
142 item = new VariableBoolItem(QStringLiteral("byte-order-mark"), docConfig->bom());
143 item->setHelpText(i18nc("short translation please", "Enable the byte order mark (BOM) when saving Unicode files."));
144 listview->addItem(item);
145
146 // Add 'bracket-highlight-color' to list
147 item = new VariableColorItem(QStringLiteral("bracket-highlight-color"), rendererConfig->highlightedBracketColor());
148 item->setHelpText(i18nc("short translation please", "Set the color for the bracket highlight."));
149 listview->addItem(item);
150
151 // Add 'current-line-color' to list
152 item = new VariableColorItem(QStringLiteral("current-line-color"), rendererConfig->highlightedLineColor());
153 item->setHelpText(i18nc("short translation please", "Set the background color for the current line."));
154 listview->addItem(item);
155
156 // Add 'default-dictionary' to list
157 Sonnet::Speller speller;
158 item = new VariableSpellCheckItem(QStringLiteral("default-dictionary"), speller.defaultLanguage());
159 item->setHelpText(i18nc("short translation please", "Set the default dictionary used for spell checking."));
160 listview->addItem(item);
161
162 // Add 'dynamic-word-wrap' to list
163 item = new VariableBoolItem(QStringLiteral("dynamic-word-wrap"), viewConfig->dynWordWrap());
164 item->setHelpText(i18nc("short translation please", "Enable dynamic word wrap of long lines."));
165 listview->addItem(item);
166
167 // Add 'end-of-line' (eol) to list
168 item = new VariableStringListItem(QStringLiteral("end-of-line"),
169 {QStringLiteral("unix"), QStringLiteral("mac"), QStringLiteral("dos")},
170 docConfig->eolString());
171 item->setHelpText(i18nc("short translation please", "Sets the end of line mode."));
172 listview->addItem(item);
173
174 // Add 'enter-to-insert-completion' to list
175 item = new VariableBoolItem(QStringLiteral("enter-to-insert-completion"), viewConfig->value(key: KateViewConfig::EnterToInsertCompletion).toBool());
176 item->setHelpText(i18nc("short translation please", "Enable whether pressing enter will insert selected auto completion item."));
177 listview->addItem(item);
178
179 // Add 'folding-markers' to list
180 item = new VariableBoolItem(QStringLiteral("folding-markers"), viewConfig->foldingBar());
181 item->setHelpText(i18nc("short translation please", "Enable folding markers in the editor border."));
182 listview->addItem(item);
183
184 // Add 'folding-preview' to list
185 item = new VariableBoolItem(QStringLiteral("folding-preview"), viewConfig->foldingPreview());
186 item->setHelpText(i18nc("short translation please", "Enable folding preview in the editor border."));
187 listview->addItem(item);
188
189 // Add 'font-size' to list
190 item = new VariableIntItem(QStringLiteral("font-size"), rendererConfig->baseFont().pointSize());
191 static_cast<VariableIntItem *>(item)->setRange(minValue: 4, maxValue: 128);
192 item->setHelpText(i18nc("short translation please", "Set the point size of the document font."));
193 listview->addItem(item);
194
195 // Add 'font' to list
196 item = new VariableFontItem(QStringLiteral("font"), rendererConfig->baseFont());
197 item->setHelpText(i18nc("short translation please", "Set the font of the document."));
198 listview->addItem(item);
199
200 // Add 'syntax' (hl) to list
201 /* Prepare list of highlighting modes */
202 QStringList hls;
203 hls.reserve(asize: KateHlManager::self()->modeList().size());
204 const auto modeList = KateHlManager::self()->modeList();
205 for (const auto &hl : modeList) {
206 hls << hl.name();
207 }
208
209 item = new VariableStringListItem(QStringLiteral("syntax"), hls, hls.at(i: 0));
210 if (activeDoc) {
211 static_cast<VariableStringListItem *>(item)->setValue(activeDoc->highlightingMode());
212 }
213 item->setHelpText(i18nc("short translation please", "Set the syntax highlighting."));
214 listview->addItem(item);
215
216 // Add 'icon-bar-color' to list
217 item = new VariableColorItem(QStringLiteral("icon-bar-color"), rendererConfig->iconBarColor());
218 item->setHelpText(i18nc("short translation please", "Set the icon bar color."));
219 listview->addItem(item);
220
221 // Add 'icon-border' to list
222 item = new VariableBoolItem(QStringLiteral("icon-border"), viewConfig->iconBar());
223 item->setHelpText(i18nc("short translation please", "Enable the icon border in the editor view."));
224 listview->addItem(item);
225
226 // Add 'indent-mode' to list
227 item = new VariableStringListItem(QStringLiteral("indent-mode"), KateAutoIndent::listIdentifiers(), docConfig->indentationMode());
228 item->setHelpText(i18nc("short translation please", "Set the auto indentation style."));
229 listview->addItem(item);
230
231 // Add 'indent-pasted-text' to list
232 item = new VariableBoolItem(QStringLiteral("indent-pasted-text"), docConfig->indentPastedText());
233 item->setHelpText(i18nc("short translation please", "Adjust indentation of text pasted from the clipboard."));
234 listview->addItem(item);
235
236 // Add 'indent-width' to list
237 item = new VariableIntItem(QStringLiteral("indent-width"), docConfig->indentationWidth());
238 static_cast<VariableIntItem *>(item)->setRange(minValue: 1, maxValue: 200);
239 item->setHelpText(i18nc("short translation please", "Set the indentation depth for each indent level."));
240 listview->addItem(item);
241
242 // Add 'keep-extra-spaces' to list
243 item = new VariableBoolItem(QStringLiteral("keep-extra-spaces"), docConfig->keepExtraSpaces());
244 item->setHelpText(i18nc("short translation please", "Allow odd indentation level (no multiple of indent width)."));
245 listview->addItem(item);
246
247 // Add 'line-numbers' to list
248 item = new VariableBoolItem(QStringLiteral("line-numbers"), viewConfig->lineNumbers());
249 item->setHelpText(i18nc("short translation please", "Show line numbers."));
250 listview->addItem(item);
251
252 // Add 'newline-at-eof' to list
253 item = new VariableBoolItem(QStringLiteral("newline-at-eof"), docConfig->ovr());
254 item->setHelpText(i18nc("short translation please", "Insert newline at end of file on save."));
255 listview->addItem(item);
256
257 // Add 'overwrite-mode' to list
258 item = new VariableBoolItem(QStringLiteral("overwrite-mode"), docConfig->ovr());
259 item->setHelpText(i18nc("short translation please", "Enable overwrite mode in the document."));
260 listview->addItem(item);
261
262 // Add 'persistent-selection' to list
263 item = new VariableBoolItem(QStringLiteral("persistent-selection"), viewConfig->persistentSelection());
264 item->setHelpText(i18nc("short translation please", "Enable persistent text selection."));
265 listview->addItem(item);
266
267 // Add 'replace-tabs-save' to list
268 item = new VariableBoolItem(QStringLiteral("replace-tabs-save"), false);
269 item->setHelpText(i18nc("short translation please", "Replace tabs with spaces when saving the document."));
270 listview->addItem(item);
271
272 // Add 'replace-tabs' to list
273 item = new VariableBoolItem(QStringLiteral("replace-tabs"), docConfig->replaceTabsDyn());
274 item->setHelpText(i18nc("short translation please", "Replace tabs with spaces."));
275 listview->addItem(item);
276
277 // Add 'remove-trailing-spaces' to list
278 item = new VariableRemoveSpacesItem(QStringLiteral("remove-trailing-spaces"), docConfig->removeSpaces());
279 item->setHelpText(i18nc("short translation please", "Remove trailing spaces when saving the document."));
280 listview->addItem(item);
281
282 // Add 'scrollbar-minimap' to list
283 item = new VariableBoolItem(QStringLiteral("scrollbar-minimap"), viewConfig->scrollBarMiniMap());
284 item->setHelpText(i18nc("short translation please", "Show scrollbar minimap."));
285 listview->addItem(item);
286
287 // Add 'scrollbar-preview' to list
288 item = new VariableBoolItem(QStringLiteral("scrollbar-preview"), viewConfig->scrollBarPreview());
289 item->setHelpText(i18nc("short translation please", "Show scrollbar preview."));
290 listview->addItem(item);
291
292 // Add 'scheme' to list
293 QStringList themeNames;
294 const auto sortedThemes = KateHlManager::self()->sortedThemes();
295 themeNames.reserve(asize: sortedThemes.size());
296 for (const auto &theme : sortedThemes) {
297 themeNames.append(t: theme.name());
298 }
299 item = new VariableStringListItem(QStringLiteral("scheme"), themeNames, rendererConfig->schema());
300 item->setHelpText(i18nc("short translation please", "Set the color scheme."));
301 listview->addItem(item);
302
303 // Add 'selection-color' to list
304 item = new VariableColorItem(QStringLiteral("selection-color"), rendererConfig->selectionColor());
305 item->setHelpText(i18nc("short translation please", "Set the text selection color."));
306 listview->addItem(item);
307
308 // Add 'show-tabs' to list
309 item = new VariableBoolItem(QStringLiteral("show-tabs"), docConfig->showTabs());
310 item->setHelpText(i18nc("short translation please", "Visualize tabs and trailing spaces."));
311 listview->addItem(item);
312
313 // Add 'smart-home' to list
314 item = new VariableBoolItem(QStringLiteral("smart-home"), docConfig->smartHome());
315 item->setHelpText(i18nc("short translation please", "Enable smart home navigation."));
316 listview->addItem(item);
317
318 // Add 'tab-indents' to list
319 item = new VariableBoolItem(QStringLiteral("tab-indents"), docConfig->tabIndentsEnabled());
320 item->setHelpText(i18nc("short translation please", "Pressing TAB key indents."));
321 listview->addItem(item);
322
323 // Add 'tab-width' to list
324 item = new VariableIntItem(QStringLiteral("tab-width"), docConfig->tabWidth());
325 static_cast<VariableIntItem *>(item)->setRange(minValue: 1, maxValue: 200);
326 item->setHelpText(i18nc("short translation please", "Set the tab display width."));
327 listview->addItem(item);
328
329 // Add 'undo-steps' to list
330 item = new VariableIntItem(QStringLiteral("undo-steps"), 0);
331 static_cast<VariableIntItem *>(item)->setRange(minValue: 0, maxValue: 100);
332 item->setHelpText(i18nc("short translation please", "Set the number of undo steps to remember (0 equals infinity)."));
333 listview->addItem(item);
334
335 // Add 'word-wrap-column' to list
336 item = new VariableIntItem(QStringLiteral("word-wrap-column"), docConfig->wordWrapAt());
337 static_cast<VariableIntItem *>(item)->setRange(minValue: 20, maxValue: 200);
338 item->setHelpText(i18nc("short translation please", "Set the word wrap column."));
339 listview->addItem(item);
340
341 // Add 'word-wrap-marker-color' to list
342 item = new VariableColorItem(QStringLiteral("word-wrap-marker-color"), rendererConfig->wordWrapMarkerColor());
343 item->setHelpText(i18nc("short translation please", "Set the word wrap marker color."));
344 listview->addItem(item);
345
346 // Add 'word-wrap' to list
347 item = new VariableBoolItem(QStringLiteral("word-wrap"), docConfig->wordWrap());
348 item->setHelpText(i18nc("short translation please", "Enable word wrap while typing text."));
349 listview->addItem(item);
350}
351
352void VariableLineEdit::setText(const QString &text)
353{
354 m_lineedit->setText(text);
355}
356
357void VariableLineEdit::clear()
358{
359 m_lineedit->clear();
360}
361
362QString VariableLineEdit::text()
363{
364 return m_lineedit->text();
365}
366
367#include "moc_variablelineedit.cpp"
368

source code of ktexteditor/src/variableeditor/variablelineedit.cpp