]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiCompare.cpp
Adds the user interface for the new Comparison feature.
[lyx.git] / src / frontends / qt4 / GuiCompare.cpp
1 /**\r
2  * \file GuiCompare.cpp\r
3  * This file is part of LyX, the document processor.\r
4  * Licence details can be found in the file COPYING.\r
5  *\r
6  * \author Vincent van Ravesteijn\r
7  *\r
8  * Full author contact details are available in file CREDITS.\r
9  */\r
10 \r
11 #include <config.h>\r
12 \r
13 #include "GuiCompare.h"\r
14 \r
15 #include "Buffer.h"\r
16 #include "BufferView.h"\r
17 #include "BufferList.h"\r
18 #include "buffer_funcs.h"\r
19 #include "FuncRequest.h"\r
20 #include "GuiView.h"\r
21 #include "LyXRC.h"\r
22 #include "qt_helpers.h"\r
23 \r
24 #include "frontends/alert.h"\r
25 \r
26 #include "support/debug.h"\r
27 #include "support/filetools.h"\r
28 #include "support/FileName.h"\r
29 #include "support/gettext.h"\r
30 \r
31 #include <QThread>\r
32 \r
33 \r
34 using namespace std;\r
35 using namespace lyx::support;\r
36 \r
37 namespace lyx {\r
38 namespace frontend {\r
39 \r
40 \r
41 GuiCompare::GuiCompare(GuiView & lv)\r
42         : GuiDialog(lv, "compare", qt_("Compare LyX files")),\r
43         compare_(0), dest_buffer_(0)\r
44 {\r
45         setupUi(this);\r
46         setModal(Qt::WindowModal);\r
47 \r
48         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));\r
49         connect(closePB, SIGNAL(clicked()), this, SLOT(slotCancel()));\r
50 \r
51         connect(newFilePB, SIGNAL(clicked()), this, SLOT(select_newfile()));\r
52         connect(oldFilePB, SIGNAL(clicked()), this, SLOT(select_oldfile()));\r
53 \r
54         connect(newFileCB, SIGNAL(currentIndexChanged(int)),\r
55                 this, SLOT(change_adaptor()));\r
56         connect(newFileCB, SIGNAL(editTextChanged(const QString &)),\r
57                 this, SLOT(change_adaptor()));\r
58         connect(oldFileCB, SIGNAL(currentIndexChanged(int)),\r
59                 this, SLOT(change_adaptor()));\r
60         connect(oldFileCB, SIGNAL(editTextChanged(const QString &)),\r
61                 this, SLOT(change_adaptor()));\r
62 \r
63         newSettingsRB->setChecked(true);\r
64 \r
65         progressBar->setValue(0);\r
66         progressBar->setEnabled(false);\r
67 \r
68         bc().setPolicy(ButtonPolicy::OkApplyCancelPolicy);\r
69         bc().setOK(okPB);\r
70 }\r
71 \r
72 GuiCompare::~GuiCompare()\r
73 {\r
74 }\r
75 \r
76 void GuiCompare::closeEvent(QCloseEvent *)\r
77 {\r
78         slotCancel();   \r
79 }\r
80 \r
81 \r
82 void GuiCompare::change_adaptor()\r
83 {\r
84         changed();\r
85 }\r
86 \r
87 \r
88 bool GuiCompare::isValid()\r
89 {\r
90         bool const valid = !newFileCB->currentText().isEmpty()\r
91                 && !oldFileCB->currentText().isEmpty();\r
92         return valid;\r
93 }\r
94 \r
95 \r
96 void GuiCompare::updateContents()\r
97 {\r
98         QString restore_filename1 = newFileCB->currentText();\r
99         QString restore_filename2 = oldFileCB->currentText();\r
100         newFileCB->clear();\r
101         oldFileCB->clear();\r
102         progressBar->setValue(0);\r
103         BufferList::iterator it = theBufferList().begin();\r
104         BufferList::iterator const end = theBufferList().end();\r
105         for (; it != end; ++it) {\r
106                 QString filename = toqstr((*it)->absFileName());\r
107                 newFileCB->addItem(filename);\r
108                 oldFileCB->addItem(filename);\r
109         }\r
110         if (lyxview().documentBufferView())\r
111                 newFileCB->setEditText(toqstr(buffer().absFileName()));\r
112         else\r
113                 newFileCB->setEditText(restore_filename1);\r
114 \r
115         if (!restore_filename2.isEmpty())\r
116                 oldFileCB->setEditText(restore_filename2);\r
117         else\r
118                 oldFileCB->clearEditText();\r
119 \r
120         if (isValid()) {\r
121                 bc().setValid(isValid());\r
122                 bc().apply();\r
123         }\r
124 }\r
125 \r
126 \r
127 void GuiCompare::select_newfile()\r
128 {\r
129         QString name = browse(newFileCB->currentText());\r
130         if (!name.isEmpty())\r
131                 newFileCB->setEditText(name);\r
132         changed();\r
133 }\r
134 \r
135 \r
136 void GuiCompare::select_oldfile()\r
137 {\r
138         QString name = browse(oldFileCB->currentText());\r
139         if (!name.isEmpty())\r
140                 oldFileCB->setEditText(name);\r
141         changed();\r
142 }\r
143 \r
144 \r
145 QString GuiCompare::browse(QString const & in_name) const\r
146 {\r
147         QString const title = qt_("Select document");\r
148 \r
149         QStringList const & filters = fileFilters(qt_("LyX Documents (*.lyx)"));\r
150         \r
151         QString filename;\r
152         if (lyxview().documentBufferView()) {\r
153                 QString path = bufferFilepath();\r
154                 filename = browseRelFile(in_name, path, title, filters, false, \r
155                         qt_("Documents|#o#O"), toqstr(lyxrc.document_path));\r
156         } else {\r
157                 QString path = toqstr(lyxrc.document_path);\r
158                 QString rel_filename = browseRelFile(in_name, path, title, filters, false, \r
159                         qt_("Documents|#o#O"), toqstr(lyxrc.document_path));\r
160                 filename = makeAbsPath(rel_filename, path);\r
161         }\r
162         return filename;        \r
163 }\r
164 \r
165 \r
166 void GuiCompare::enableControls(bool enable) const\r
167 {\r
168         newFileLA->setEnabled(enable);\r
169         newFilePB->setEnabled(enable);\r
170         newFileCB->setEnabled(enable);\r
171         oldFileLA->setEnabled(enable);\r
172         oldFilePB->setEnabled(enable);\r
173         oldFileCB->setEnabled(enable);\r
174         okPB->setEnabled(enable);\r
175         groupBox->setEnabled(enable);\r
176         progressBar->setEnabled(!enable);\r
177 \r
178         if (enable)\r
179                 closePB->setText(qt_("Close"));\r
180         else\r
181                 closePB->setText(qt_("Cancel"));\r
182 }\r
183 \r
184 \r
185 void GuiCompare::finished(bool aborted)\r
186 {\r
187         enableControls(true);\r
188         \r
189         if (aborted) {\r
190                 dest_buffer_->markClean();\r
191                 theBufferList().release(dest_buffer_);\r
192                 setWindowTitle(window_title_);\r
193                 progressBar->setValue(0);\r
194         } else {\r
195                 hideView();\r
196                 bc().ok();\r
197                 dispatch(FuncRequest(LFUN_BUFFER_SWITCH, dest_buffer_->absFileName()));\r
198         }\r
199 }\r
200 \r
201 \r
202 void GuiCompare::nextIt(int val)\r
203 {\r
204         progressBar->setValue(progressBar->value() + val);\r
205 }\r
206 \r
207 \r
208 void GuiCompare::progress_max(int max) const\r
209 {\r
210         progressBar->setMaximum(max);\r
211 }\r
212         \r
213 \r
214 void GuiCompare::slotOK()\r
215 {\r
216         enableControls(false);\r
217         if (!run()) {\r
218                 Alert::error(_("Error"),\r
219                         _("Unable to compare files."));\r
220                 finished(true);\r
221         }\r
222 }\r
223 \r
224 \r
225 void GuiCompare::slotCancel()\r
226 {\r
227         GuiDialog::slotClose();\r
228         progressBar->setValue(0);\r
229 }\r
230 \r
231 \r
232 Buffer const * GuiCompare::bufferFromFileName(string const & file) const\r
233 {\r
234         FileName fname;\r
235         if (FileName::isAbsolute(file))\r
236                 fname.set(file);\r
237         else if (lyxview().documentBufferView())\r
238                 fname = support::makeAbsPath(file, fromqstr(bufferFilepath()));\r
239 \r
240         if (fname.empty()\r
241                         || (!fname.exists() && !theBufferList().getBuffer(fname))) {\r
242                 LYXERR0( "Unable to read: " << file);\r
243                 return 0;\r
244         }\r
245         return loadIfNeeded(fname);\r
246 }\r
247 \r
248 \r
249 int GuiCompare::run()\r
250 {\r
251         progressBar->setValue(0);\r
252 \r
253         new_buffer_ = bufferFromFileName(fromqstr(newFileCB->currentText()));\r
254         old_buffer_ = bufferFromFileName(fromqstr(oldFileCB->currentText()));\r
255 \r
256         // new buffer that will carry the output\r
257         FileName initpath(lyxrc.document_path);\r
258         dest_buffer_ = newUnnamedFile(initpath, to_utf8(_("differences")));\r
259         dest_buffer_->changed();\r
260         dest_buffer_->markDirty();\r
261 \r
262         return 0;\r
263 }\r
264 \r
265 \r
266 Dialog * createGuiCompare(GuiView & lv) { return new GuiCompare(lv); }\r
267 \r
268 \r
269 } // namespace frontend\r
270 } // namespace lyx\r
271 \r
272 \r
273 #include "moc_GuiCompare.cpp"\r