]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiTabularCreate.cpp
Introducing table templates
[lyx.git] / src / frontends / qt4 / GuiTabularCreate.cpp
1 /**
2  * \file GuiTabularCreate.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "GuiTabularCreate.h"
14
15 #include "EmptyTable.h"
16 #include "FuncRequest.h"
17
18 #include "support/debug.h"
19 #include "support/convert.h"
20 #include "support/filetools.h"
21 #include "support/gettext.h"
22 #include "support/lstrings.h"
23 #include "support/qstring_helpers.h"
24 #include "support/Package.h"
25
26 #include <QDirIterator>
27 #include <QSpinBox>
28 #include <QPushButton>
29
30 using namespace std;
31 using namespace lyx::support;
32
33 namespace lyx {
34 namespace frontend {
35
36 void GuiTabularCreate::getFiles()
37 {
38         // We look for lyx files in the subdirectory dir of
39         //   1) user_lyxdir
40         //   2) build_lyxdir (if not empty)
41         //   3) system_lyxdir
42         // in this order. Files with a given sub-hierarchy will
43         // only be listed once.
44         // We also consider i18n subdirectories and store them separately.
45         QStringList dirs;
46
47         // The three locations to look at.
48         string const user = addPath(package().user_support().absFileName(), "tabletemplates");
49         string const build = addPath(package().build_support().absFileName(), "tabletemplates");
50         string const system = addPath(package().system_support().absFileName(), "tabletemplates");
51
52         dirs << toqstr(user)
53              << toqstr(build)
54              << toqstr(system);
55
56         for (int i = 0; i < dirs.size(); ++i) {
57                 QString const dir = dirs.at(i);
58                 QDirIterator it(dir, QDir::Files, QDirIterator::Subdirectories);
59                 while (it.hasNext()) {
60                         QString fn = QFileInfo(it.next()).fileName();
61                         if (!fn.endsWith(".lyx") || fn.contains("_1x"))
62                                 continue;
63                         QString data = fn.left(fn.lastIndexOf(".lyx"));
64                         QString guiname = data;
65                         guiname = toqstr(translateIfPossible(qstring_to_ucs4(guiname.replace('_', ' '))));
66                         QString relpath = toqstr(makeRelPath(qstring_to_ucs4(fn),
67                                                              qstring_to_ucs4(dir)));
68                         if (styleCO->findData(data) == -1)
69                                 styleCO->addItem(guiname, data);
70                 }
71         }
72 }
73
74 GuiTabularCreate::GuiTabularCreate(GuiView & lv)
75         : GuiDialog(lv, "tabularcreate", qt_("Insert Table"))
76 {
77         setupUi(this);
78
79         rowsSB->setValue(5);
80         columnsSB->setValue(5);
81         table->setMinimumSize(100, 100);
82
83         connect(table, SIGNAL(rowsChanged(int)),
84                 rowsSB, SLOT(setValue(int)));
85         connect(table, SIGNAL(colsChanged(int)),
86                 columnsSB, SLOT(setValue(int)));
87         connect(rowsSB, SIGNAL(valueChanged(int)),
88                 table, SLOT(setNumberRows(int)));
89         connect(columnsSB, SIGNAL(valueChanged(int)),
90                 table, SLOT(setNumberColumns(int)));
91
92         connect(buttonBox, SIGNAL(clicked(QAbstractButton *)),
93                 this, SLOT(slotButtonBox(QAbstractButton *)));
94
95         connect(rowsSB, SIGNAL(valueChanged(int)),
96                 this, SLOT(rowsChanged(int)));
97         connect(columnsSB, SIGNAL(valueChanged(int)),
98                 this, SLOT(columnsChanged(int)));
99
100         bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
101         bc().setOK(buttonBox->button(QDialogButtonBox::Ok));
102         bc().setApply(buttonBox->button(QDialogButtonBox::Apply));
103         bc().setCancel(buttonBox->button(QDialogButtonBox::Cancel));
104         bc().setValid(isValid());
105
106         // Fill styles combo
107         styleCO->addItem(qt_("Default"), toqstr("default"));
108         getFiles();
109 }
110
111
112 void GuiTabularCreate::on_styleCO_activated(int i)
113 {
114         style_ = styleCO->itemData(i).toString();
115         changed();
116 }
117
118
119 void GuiTabularCreate::columnsChanged(int)
120 {
121         changed();
122 }
123
124
125 void GuiTabularCreate::rowsChanged(int)
126 {
127         changed();
128 }
129
130
131 void GuiTabularCreate::applyView()
132 {
133         params_.first = rowsSB->value();
134         params_.second = columnsSB->value();
135 }
136
137
138 bool GuiTabularCreate::initialiseParams(string const &)
139 {
140         params_.first  = 5;
141         params_.second = 5;
142         style_ = styleCO->itemData(styleCO->currentIndex()).toString();
143         return true;
144 }
145
146
147 void GuiTabularCreate::clearParams()
148 {
149         params_.first  = 0;
150         params_.second = 0;
151 }
152
153
154 void GuiTabularCreate::dispatchParams()
155 {
156         string sdata;
157         if (style_ != "default")
158                 sdata = fromqstr(style_) + ' ';
159         sdata += convert<string>(params().first) + ' ' + convert<string>(params().second);
160         dispatch(FuncRequest(getLfun(), sdata));
161 }
162
163
164 FuncCode GuiTabularCreate::getLfun() const
165 {
166         if (style_.isEmpty() || style_ == "default")
167                 return  LFUN_TABULAR_INSERT;
168         
169         return LFUN_TABULAR_STYLE_INSERT;
170 }
171
172
173 Dialog * createGuiTabularCreate(GuiView & lv)
174 {
175         return new GuiTabularCreate(lv);
176 }
177
178
179 } // namespace frontend
180 } // namespace lyx
181
182 #include "moc_GuiTabularCreate.cpp"