]> git.lyx.org Git - features.git/blob - src/frontends/qt4/GuiInfo.cpp
fc71f85d87a42d6a472ece86ac150c03fae7b481
[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  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "GuiInfo.h"
14
15 #include "qt_helpers.h"
16
17 #include "Buffer.h"
18 #include "buffer_funcs.h"
19 #include "BufferParams.h"
20 #include "BufferView.h"
21 #include "Cursor.h"
22 #include "FuncRequest.h"
23
24 #include "insets/InsetInfo.h"
25
26 #include "support/debug.h"
27 #include "support/lstrings.h"
28
29
30 using namespace std;
31 using namespace lyx::support;
32
33 namespace lyx {
34 namespace frontend {
35
36 /////////////////////////////////////////////////////////////////
37 //
38 // GuiInfo
39 //
40 /////////////////////////////////////////////////////////////////
41
42 char const * info_types[] =
43 { "unknown", "shortcut", "shortcuts", "lyxrc", "package", "textclass", "menu", "icon", "buffer", "" };
44
45 char const * info_types_gui[] =
46 { N_("unknown"), N_("shortcut"), N_("shortcuts"), N_("lyxrc"), N_("package"), N_("textclass"),
47   N_("menu"), N_("icon"), N_("buffer"), ""};
48
49
50 GuiInfo::GuiInfo(GuiView & lv)
51         : DialogView(lv, "info", qt_("Info"))
52 {
53         setupUi(this);
54
55         typeCO->blockSignals(true);
56         for (int n = 0; info_types[n][0]; ++n)
57                 typeCO->addItem(qt_(info_types_gui[n]));
58         typeCO->blockSignals(false);
59 }
60
61
62 void GuiInfo::on_newPB_clicked()
63 {
64         dialogToParams();
65         docstring const argument = qstring_to_ucs4(type_ + ' ' + name_);
66         dispatch(FuncRequest(LFUN_INFO_INSERT, argument));
67 }
68
69
70 void GuiInfo::on_closePB_clicked()
71 {
72         hide();
73 }
74
75
76 void GuiInfo::on_typeCO_currentIndexChanged(int)
77 {
78         applyView();
79 }
80
81
82 void GuiInfo::on_nameLE_textChanged(QString const &)
83 {
84         applyView();
85 }
86
87
88 void GuiInfo::applyView()
89 {
90         InsetInfo const * ii = static_cast<InsetInfo const *>(inset(INFO_CODE));
91         if (!ii) {
92                 return;
93         }
94         
95         dialogToParams();
96         docstring const argument = qstring_to_ucs4(type_ + ' ' + name_);
97         if (!ii->validate(argument))
98                 return;
99
100         dispatch(FuncRequest(LFUN_INSET_MODIFY, argument));
101         // FIXME: update the inset contents
102         bufferview()->buffer().updateLabels(false);
103         BufferView * bv = const_cast<BufferView *>(bufferview());
104         bv->updateMetrics();
105         bv->buffer().changed();
106         bv->buffer().markDirty();
107 }
108
109
110 void GuiInfo::updateView()
111 {
112         InsetInfo const * ii = static_cast<InsetInfo const *>(inset(INFO_CODE));
113         if (!ii) {
114                 enableView(false);
115                 return;
116         }
117
118         type_ = toqstr(ii->infoType());
119         name_ = toqstr(ii->infoName());
120         paramsToDialog();
121 }
122
123
124 void GuiInfo::paramsToDialog()
125 {
126         typeCO->blockSignals(true);
127         nameLE->blockSignals(true);
128         int type = findToken(info_types, fromqstr(type_));
129         typeCO->setCurrentIndex(type >= 0 ? type : 0);
130         // Without this test, 'math-insert' (name_) will replace 'math-insert '
131         // in nameLE and effectively disallow the input of spaces after a LFUN.
132         if (nameLE->text().trimmed() != name_)
133                 nameLE->setText(name_);
134         typeCO->blockSignals(false);
135         nameLE->blockSignals(false);
136 }
137
138
139 void GuiInfo::dialogToParams()
140 {
141         int type = typeCO->currentIndex();
142         if (type != -1)
143                 type_ = info_types[type];
144         name_ = nameLE->text();
145 }
146
147
148 void GuiInfo::enableView(bool enable)
149 {
150         typeCO->setEnabled(enable);
151         nameLE->setEnabled(enable);
152         newPB->setEnabled(!enable);
153 }
154
155
156 Dialog * createGuiInfo(GuiView & lv) { return new GuiInfo(lv); }
157
158
159 } // namespace frontend
160 } // namespace lyx
161
162 #include "moc_GuiInfo.cpp"