]> git.lyx.org Git - features.git/blob - src/frontends/qt4/GuiInfo.cpp
Fix small UI glitch
[features.git] / src / frontends / qt4 / GuiInfo.cpp
1 /**
2  * \file GuiInfo.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Abdelrazak Younes
7  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "GuiInfo.h"
15
16 #include "qt_helpers.h"
17
18 #include "Buffer.h"
19 #include "buffer_funcs.h"
20 #include "BufferParams.h"
21 #include "BufferView.h"
22 #include "Cursor.h"
23 #include "Language.h"
24
25 #include "GuiApplication.h"
26 #include "GuiView.h"
27
28 #include "insets/InsetInfo.h"
29
30 #include "support/debug.h"
31 #include "support/gettext.h"
32 #include "support/lstrings.h"
33
34
35 using namespace std;
36 using namespace lyx::support;
37
38 namespace lyx {
39 namespace frontend {
40
41 /////////////////////////////////////////////////////////////////
42 //
43 // GuiInfo
44 //
45 /////////////////////////////////////////////////////////////////
46
47 char const * info_types[] =
48 { "date",
49   "moddate",
50   "fixdate",
51   "time",
52   "modtime",
53   "fixtime",
54   "buffer",
55   "vcs",
56   "package",
57   "textclass",
58   "shortcut",
59   "shortcuts",
60   "menu",
61   "icon",
62   "lyxrc",
63   "lyxinfo",
64   "unknown",
65   ""
66 };
67
68 // GUI names (in combo)
69 char const * info_types_gui[] =
70 { N_("Date (current)"),// date
71   N_("Date (last modified)"),// moddate
72   N_("Date (fix)"),// fixdate
73   N_("Time (current)"),// time
74   N_("Time (last modified)"),// modtime
75   N_("Time (fix)"),// fixtime
76   N_("Document Information"),// buffer
77   N_("Version Control Information"),// vcs
78   N_("LaTeX Package Availability"),// package
79   N_("LaTeX Class Availability"),// textclass
80   N_("Last Assigned Keyboard Shortcut"),// shortcut
81   N_("All Keyboard Shortcuts"),// shortcuts
82   N_("LyX Menu Location"),// menu
83   N_("LyX Toolbar Icon"),// icon
84   N_("LyX Preferences Entry"),// lyxrc
85   N_("LyX Application Information"),// lyxinfo
86   N_("Unknown"),// unknown
87   ""
88 };
89
90 // Line edit label
91 char const * info_name_gui[] =
92 { N_("Custom Format"),// date
93   N_("Custom Format"),// moddate
94   N_("Custom Format"),// fixdate
95   N_("Custom Format"),// time
96   N_("Custom Format"),// modtime
97   N_("Custom Format"),// fixtime
98   N_("Not Applicable"),// buffer
99   N_("Not Applicable"),// vcs
100   N_("Package Name"),// package
101   N_("Class Name"),// textclass
102   N_("LyX Function"),// shortcut
103   N_("LyX Function"),// shortcuts
104   N_("LyX Function"),// menu
105   N_("LyX Function"),// icon
106   N_("Preferences Key"),// lyxrc
107   N_("Not Applicable"),// lyxinfo
108   N_("Not Applicable"),// unknown
109   ""
110 };
111
112 // Line edit tooltip
113 char const * info_tooltip[] =
114 { N_("Enter date format specification, using the following placeholders:\n"
115      "* d: day as number without a leading zero\n"
116      "* dd: day as number with a leading zero\n"
117      "* ddd: abbreviated localized day name\n"
118      "* dddd: long localized day name\n"
119      "* M: month as number without a leading zero\n"
120      "* MM: month as number with a leading zero\n"
121      "* MMM: abbreviated localized month name\n"
122      "* MMMM: long localized month name\n"
123      "* yy: year as two digit number\n"
124      "* yyyy: year as four digit number"),// date
125   N_("Enter date format specification, using the following placeholders:\n"
126      "* d: day as number without a leading zero\n"
127      "* dd: day as number with a leading zero\n"
128      "* ddd: abbreviated localized day name\n"
129      "* dddd: long localized day name\n"
130      "* M: month as number without a leading zero\n"
131      "* MM: month as number with a leading zero\n"
132      "* MMM: abbreviated localized month name\n"
133      "* MMMM: long localized month name\n"
134      "* yy: year as two digit number\n"
135      "* yyyy: year as four digit number"),// moddate
136   N_("Enter date format specification, using the following placeholders:\n"
137      "* d: day as number without a leading zero\n"
138      "* dd: day as number with a leading zero\n"
139      "* ddd: abbreviated localized day name\n"
140      "* dddd: long localized day name\n"
141      "* M: month as number without a leading zero\n"
142      "* MM: month as number with a leading zero\n"
143      "* MMM: abbreviated localized month name\n"
144      "* MMMM: long localized month name\n"
145      "* yy: year as two digit number\n"
146      "* yyyy: year as four digit number"),// fixdate
147   N_("Enter time format specification, using the following placeholders:\n"
148      "* h: the hour without a leading zero (1-12 in AM/PM)\n"
149      "* hh: the hour with a leading zero (01-12 in AM/PM)\n"
150      "* H: the hour without a leading zero (0-23 in AM/PM)\n"
151      "* HH: the hour with a leading zero (00-23 in AM/PM)\n"
152      "* m: the minute without a leading zero\n"
153      "* mm: the minute with a leading zero\n"
154      "* s: the second without a leading zero\n"
155      "* ss: the second with a leading zero\n"
156      "* z: the milliseconds without leading zeroes\n"
157      "* zzz: the milliseconds with leading zeroes\n"
158      "* AP or A: use AM/PM display ('AM'/'PM')\n"
159      "* ap or a: use am/pm display ('am'/'pm')\n"
160      "* t: the timezone (e.g. CEST)"),// time
161   N_("Enter time format specification, using the following placeholders:\n"
162      "* h: the hour without a leading zero (1-12 in AM/PM)\n"
163      "* hh: the hour with a leading zero (01-12 in AM/PM)\n"
164      "* H: the hour without a leading zero (0-23 in AM/PM)\n"
165      "* HH: the hour with a leading zero (00-23 in AM/PM)\n"
166      "* m: the minute without a leading zero\n"
167      "* mm: the minute with a leading zero\n"
168      "* s: the second without a leading zero\n"
169      "* ss: the second with a leading zero\n"
170      "* z: the milliseconds without leading zeroes\n"
171      "* zzz: the milliseconds with leading zeroes\n"
172      "* AP or A: use AM/PM display ('AM'/'PM')\n"
173      "* ap or a: use am/pm display ('am'/'pm')\n"
174      "* t: the timezone (e.g. CEST)"),// modtime
175   N_("Enter time format specification, using the following placeholders:\n"
176      "* h: the hour without a leading zero (1-12 in AM/PM)\n"
177      "* hh: the hour with a leading zero (01-12 in AM/PM)\n"
178      "* H: the hour without a leading zero (0-23 in AM/PM)\n"
179      "* HH: the hour with a leading zero (00-23 in AM/PM)\n"
180      "* m: the minute without a leading zero\n"
181      "* mm: the minute with a leading zero\n"
182      "* s: the second without a leading zero\n"
183      "* ss: the second with a leading zero\n"
184      "* z: the milliseconds without leading zeroes\n"
185      "* zzz: the milliseconds with leading zeroes\n"
186      "* AP or A: use AM/PM display ('AM'/'PM')\n"
187      "* ap or a: use am/pm display ('am'/'pm')\n"
188      "* t: the timezone (e.g. CEST)"),// fixtime
189   N_("Please select a valid type above"),// buffer
190   N_("Please select a valid type above"),// vcs
191   N_("Enter a LaTeX package name such as 'hyperref' (extension is optional). "
192      "The output will be 'Yes' (package available) or 'No' (package unavailable)."),// package
193   N_("Enter a LaTeX class name such as 'article' (extension is optional). "
194      "The output will be 'Yes' (class available) or 'No' (class unavailable)."),// textclass
195   N_("Enter a function name such as 'math-insert \\alpha'. Please refer to Help > LyX Functions for a comprehensive list of functions. "
196      "The output is the most recently assigned keyboard shortcut for this function"),// shortcut
197   N_("Enter a function name such as 'math-insert \\alpha'. Please refer to Help > LyX Functions for a comprehensive list of functions. "
198      "The output lists all possible keyboard shortcuts for this function"),// shortcuts
199   N_("Enter a function name such as 'math-insert \\alpha'. Please refer to Help > LyX Functions for a comprehensive list of functions. "
200      "The output is the path to the function in the menu (using the current localization)."),// menu
201   N_("Enter a function name such as 'math-insert \\alpha'. Please refer to Help > LyX Functions for a comprehensive list of functions. "
202      "The output is the toolbar icon for this function (using the active icon theme)."),// icon
203   N_("Enter a LyX preferences key such as 'bind_file'. See the proposed list for available entries. "
204      "The output is the current setting of this preference."),// lyxrc
205   N_("Please select a valid type above"),// lyxinfo
206   N_("Please select a valid type above"),// unknown
207   ""};
208
209
210 GuiInfo::GuiInfo(QWidget * parent) : InsetParamsWidget(parent)
211 {
212         setupUi(this);
213
214         typeCO->blockSignals(true);
215         for (int n = 0; info_types[n][0]; ++n)
216                 typeCO->addItem(qt_(info_types_gui[n]), info_types[n]);
217         typeCO->blockSignals(false);
218
219         connect(typeCO, SIGNAL(currentIndexChanged(int)), this, SLOT(updateArguments(int)));
220         connect(nameLE, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
221         connect(fixDateLE, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
222         connect(infoLW, SIGNAL(currentTextChanged(QString)), this, SIGNAL(changed()));
223 }
224
225
226 void GuiInfo::paramsToDialog(Inset const * inset)
227 {
228         InsetInfo const * ii = static_cast<InsetInfo const *>(inset);
229         params_ = ii->params();
230         QString const type = toqstr(params_.infoType());
231         QString name = toqstr(params_.name);
232         QString fixdate;
233         if (type == "fixdate" || type == "fixtime") {
234                 fixdate = name.section('@', 1, 1);
235                 name = name.section('@', 0, 0);
236                 if (!fixdate.isEmpty())
237                         fixDateLE->setText(fixdate);
238                 else {
239                         if (type == "fixdate")
240                                 fixDateLE->setText(QDate::currentDate().toString(Qt::ISODate));
241                         else
242                                 fixDateLE->setText(QTime::currentTime().toString(Qt::ISODate));
243                 }
244         }
245         typeCO->blockSignals(true);
246         nameLE->blockSignals(true);
247         nameLE->clear();
248         int const i = typeCO->findData(type);
249         typeCO->setCurrentIndex(i);
250         updateArguments(i);
251         int argindex = -1;
252         int customindex = 0;
253         for (int i = 0 ; i < infoLW->count() ; ++i) {
254                 if (infoLW->item(i)->data(Qt::UserRole).toString() == name)
255                         argindex = i;
256                 else if (infoLW->item(i)->data(Qt::UserRole).toString() == "custom")
257                         customindex = i;
258         }
259         if (argindex != -1)
260                 infoLW->setCurrentRow(argindex);
261         else {
262                 // Without this test, 'math-insert' (name) will replace 'math-insert '
263                 // in nameLE and effectively disallow the input of spaces after a LFUN.
264                 if (nameLE->text().trimmed() != name)
265                         nameLE->setText(name);
266                 infoLW->setCurrentRow(customindex);
267         }
268         typeCO->blockSignals(false);
269         nameLE->blockSignals(false);
270 }
271
272
273 docstring GuiInfo::dialogToParams() const
274 {
275         QString type =
276                 typeCO->itemData(typeCO->currentIndex()).toString();
277         QString name = infoLW->currentItem() ?
278                                 infoLW->currentItem()->data(Qt::UserRole).toString()
279                               : QString();
280         if (name == "custom")
281                 name = nameLE->text();
282         if (type == "fixdate" || type == "fixtime") {
283                 QString fd = fixDateLE->text();
284                 if (fd.isEmpty())
285                         fd = (type == "fixdate") ?
286                                         QDate::currentDate().toString(Qt::ISODate)
287                                       : QTime::currentTime().toString(Qt::ISODate);
288                 name += "@" + fd;
289         }
290         return qstring_to_ucs4(type + ' ' + name);
291 }
292
293
294 bool GuiInfo::initialiseParams(std::string const & sdata)
295 {
296         Language const * lang = languages.getLanguage(sdata);
297         if (!lang)
298                 return false;
299         params_.lang = lang;
300         updateArguments(typeCO->currentIndex());
301         return true;
302 }
303
304
305 void GuiInfo::updateArguments(int i)
306 {
307         if (i == -1)
308                 i = 0;
309
310         infoLW->clear();
311         BufferView const * bv = guiApp->currentView()->currentBufferView();
312         vector<pair<string,docstring>> args = params_.getArguments(const_cast<Buffer *>(&bv->buffer()),
313                                                                    info_types[i]);
314         for (auto const & p : args) {
315                 QListWidgetItem * li = new QListWidgetItem(toqstr(p.second));
316                 li->setData(Qt::UserRole, toqstr(p.first));
317                 if (p.first == "invalid")
318                         // non-selectable, disabled item!
319                         li->setFlags(Qt::NoItemFlags);
320                 if (p.first == "custom")
321                         li->setData(Qt::ToolTipRole, qt_("Enter a valid value below"));
322                 infoLW->addItem(li);
323         }
324         if (infoLW->count() > 0)
325                 infoLW->setCurrentRow(0);
326         changed();
327 }
328
329
330 bool GuiInfo::checkWidgets(bool readonly) const
331 {
332         nameLE->setReadOnly(readonly);
333         typeCO->setEnabled(!readonly);
334         nameLA->setText(qt_(info_name_gui[typeCO->currentIndex()]) + toqstr(":"));
335
336         QString const arg = infoLW->currentItem() ?
337                                 infoLW->currentItem()->data(Qt::UserRole).toString()
338                               : QString();
339
340         bool const type_enabled = (arg == "custom");
341         nameLA->setEnabled(type_enabled);
342         nameLE->setEnabled(type_enabled);
343         nameLE->setToolTip(qt_(info_tooltip[typeCO->currentIndex()]));
344
345         string const typestr = info_types[typeCO->currentIndex()];
346         bool const fixdate_enabled = (typestr == "fixdate" || typestr == "fixtime");
347         fixDateLE->setVisible(fixdate_enabled);
348         fixDateLA->setVisible(fixdate_enabled);
349         if (typestr == "fixdate") {
350                 fixDateLE->setToolTip(qt_("Here you can enter a fix date (in ISO format: YYYY-MM-DD)"));
351                 fixDateLA->setText(qt_("&Fix Date:"));
352         }
353         else if (typestr == "fixtime") {
354                 fixDateLE->setToolTip(qt_("Here you can enter a fix time (in ISO format: hh:mm:ss)"));
355                 fixDateLA->setText(qt_("&Fix Time:"));
356         }
357
358         if (!InsetParamsWidget::checkWidgets())
359                 return false;
360         return !arg.isEmpty() && (arg != "custom" || !nameLE->text().isEmpty());
361 }
362
363
364
365 } // namespace frontend
366 } // namespace lyx
367
368 #include "moc_GuiInfo.cpp"