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