]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiCompare.cpp
rename buffer parameter math_number_before to math_numbering_side
[lyx.git] / src / frontends / qt4 / GuiCompare.cpp
1 /**
2  * \file GuiCompare.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Vincent van Ravesteijn
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "GuiCompare.h"
14
15 #include "Buffer.h"
16 #include "BufferView.h"
17 #include "BufferList.h"
18 #include "buffer_funcs.h"
19 #include "Compare.h"
20 #include "FuncRequest.h"
21 #include "GuiView.h"
22 #include "LyXRC.h"
23 #include "qt_helpers.h"
24
25 #include "frontends/alert.h"
26
27 #include "support/debug.h"
28 #include "support/filetools.h"
29 #include "support/FileName.h"
30 #include "support/gettext.h"
31
32 #include <QThread>
33
34
35 using namespace std;
36 using namespace lyx::support;
37
38 namespace lyx {
39 namespace frontend {
40
41
42 GuiCompare::GuiCompare(GuiView & lv)
43         : GuiDialog(lv, "compare", qt_("Compare LyX files")),
44         compare_(0), dest_buffer_(0), old_buffer_(0), new_buffer_(0)
45 {
46         setupUi(this);
47         setModal(Qt::WindowModal);
48
49         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
50         connect(closePB, SIGNAL(clicked()), this, SLOT(slotCancel()));
51
52         connect(newFilePB, SIGNAL(clicked()), this, SLOT(selectNewFile()));
53         connect(oldFilePB, SIGNAL(clicked()), this, SLOT(selectOldFile()));
54
55         connect(newFileCB, SIGNAL(currentIndexChanged(int)),
56                 this, SLOT(changeAdaptor()));
57         connect(newFileCB, SIGNAL(editTextChanged(const QString &)),
58                 this, SLOT(changeAdaptor()));
59         connect(oldFileCB, SIGNAL(currentIndexChanged(int)),
60                 this, SLOT(changeAdaptor()));
61         connect(oldFileCB, SIGNAL(editTextChanged(const QString &)),
62                 this, SLOT(changeAdaptor()));
63
64         newSettingsRB->setChecked(true);
65         trackingCB->setChecked(true);
66
67         closePB->setCursor(Qt::ArrowCursor);
68
69         bc().setPolicy(ButtonPolicy::OkApplyCancelPolicy);
70         bc().setOK(okPB);
71 }
72
73 GuiCompare::~GuiCompare()
74 {
75         if (compare_)
76                 delete compare_;
77 }
78
79 void GuiCompare::closeEvent(QCloseEvent *)
80 {
81         slotCancel();   
82 }
83
84
85 void GuiCompare::changeAdaptor()
86 {
87         changed();
88 }
89
90
91 bool GuiCompare::isValid()
92 {
93         bool const valid = !newFileCB->currentText().isEmpty()
94                 && !oldFileCB->currentText().isEmpty();
95         return valid;
96 }
97
98
99 void GuiCompare::updateContents()
100 {
101         if (compare_ && compare_->isRunning())
102                 return;
103
104         QString restore_filename1 = newFileCB->currentText();
105         QString restore_filename2 = oldFileCB->currentText();
106         newFileCB->clear();
107         oldFileCB->clear();
108         progressBar->setValue(0);
109         statusBar->clearMessage();
110         BufferList::iterator it = theBufferList().begin();
111         BufferList::iterator const end = theBufferList().end();
112         for (; it != end; ++it) {
113                 QString filename = toqstr((*it)->absFileName());
114                 newFileCB->addItem(filename);
115                 oldFileCB->addItem(filename);
116         }
117         if (!restore_filename1.isEmpty())
118                 newFileCB->setEditText(restore_filename1);
119         else if (lyxview().documentBufferView())
120                 newFileCB->setEditText(toqstr(buffer().absFileName()));
121
122         if (!restore_filename2.isEmpty())
123                 oldFileCB->setEditText(restore_filename2);
124         else
125                 oldFileCB->clearEditText();
126
127         if (isValid()) {
128                 bc().setValid(isValid());
129                 bc().apply();
130         }
131 }
132
133
134 void GuiCompare::selectNewFile()
135 {
136         QString name = browse(newFileCB->currentText());
137         if (!name.isEmpty())
138                 newFileCB->setEditText(name);
139         changed();
140 }
141
142
143 void GuiCompare::selectOldFile()
144 {
145         QString name = browse(oldFileCB->currentText());
146         if (!name.isEmpty())
147                 oldFileCB->setEditText(name);
148         changed();
149 }
150
151
152 QString GuiCompare::browse(QString const & in_name) const
153 {
154         QString const title = qt_("Select document");
155
156         QStringList const & filters = fileFilters(qt_("LyX Documents (*.lyx)"));
157         
158         QString filename;
159         if (lyxview().documentBufferView()) {
160                 QString path = bufferFilePath();
161                 filename = browseRelToParent(in_name, path, title, filters, false,
162                         qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
163         } else {
164                 QString path = toqstr(lyxrc.document_path);
165                 QString rel_filename = browseRelToParent(in_name, path, title, filters, false,
166                         qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
167                 filename = makeAbsPath(rel_filename, path);
168         }
169         return filename;        
170 }
171
172
173 void GuiCompare::enableControls(bool enable)
174 {
175         // Set the hourglass cursor for the dialog, but
176         // never for the cancel button.
177         setCursor(enable ? Qt::ArrowCursor : Qt::WaitCursor);
178
179         newFileLA->setEnabled(enable);
180         newFilePB->setEnabled(enable);
181         newFileCB->setEnabled(enable);
182         oldFileLA->setEnabled(enable);
183         oldFilePB->setEnabled(enable);
184         oldFileCB->setEnabled(enable);
185         okPB->setEnabled(enable);
186         groupBox->setEnabled(enable);
187         progressBar->setEnabled(!enable);
188
189         if (enable)
190                 closePB->setText(qt_("Close"));
191         else
192                 closePB->setText(qt_("Cancel"));
193 }
194
195
196 void GuiCompare::error()
197 {
198         Alert::error(_("Error"), _("Error while comparing documents."));
199         finished(true);
200 }
201
202 void GuiCompare::finished(bool aborted)
203 {
204         enableControls(true);
205
206         if (compare_) {
207                 delete compare_;
208                 compare_ = 0;
209         }
210         
211         if (aborted) {
212                 if (dest_buffer_) {
213                         dest_buffer_->markClean();
214                         theBufferList().release(dest_buffer_);
215                 }
216                 progressBar->setValue(0);
217                 statusBar->showMessage(qt_("Aborted"), 5000);
218         } else {
219                 hideView();
220                 bc().ok();
221                 if (dest_buffer_) {
222                         dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
223                                 dest_buffer_->absFileName()));
224                         if (trackingCB->isChecked()) {
225                                 dispatch(FuncRequest(LFUN_CHANGES_OUTPUT));
226                                 dispatch(FuncRequest(LFUN_CHANGES_TRACK));
227                         }
228                 }
229                 statusBar->showMessage(qt_("Finished"), 5000);
230         }
231 }
232
233
234 void GuiCompare::progress(int val)
235 {
236         progressBar->setValue(progressBar->value() + val);
237 }
238
239
240 void GuiCompare::progressMax(int max) const
241 {
242         progressBar->setMaximum(max);
243 }
244
245
246 void GuiCompare::setStatusMessage(QString msg)
247 {
248         statusBar->showMessage(msg);
249 }
250
251
252 void GuiCompare::slotOK()
253 {
254         enableControls(false);
255         if (!run())
256                 error();
257 }
258
259
260 void GuiCompare::slotCancel()
261 {
262         if (compare_ && compare_->isRunning()) {
263                 statusBar->showMessage(qt_("Aborting process..."));
264                 compare_->abort();
265         } else {
266                 GuiDialog::slotClose();
267                 progressBar->setValue(0);
268                 statusBar->clearMessage();
269         }
270 }
271
272
273 Buffer const * GuiCompare::bufferFromFileName(string const & file) const
274 {
275         FileName fname;
276         if (FileName::isAbsolute(file))
277                 fname.set(file);
278         else if (lyxview().documentBufferView())
279                 fname = support::makeAbsPath(file, fromqstr(bufferFilePath()));
280
281         if (fname.empty()
282                         || (!fname.exists() && !theBufferList().getBuffer(fname))) {
283                 LYXERR0( "Unable to read: " << file);
284                 return 0;
285         }
286         return loadIfNeeded(fname);
287 }
288
289
290 int GuiCompare::run()
291 {
292         progressBar->setValue(0);
293
294         new_buffer_ = bufferFromFileName(fromqstr(newFileCB->currentText()));
295         old_buffer_ = bufferFromFileName(fromqstr(oldFileCB->currentText()));
296
297         // new buffer that will carry the output
298         FileName initpath(lyxrc.document_path);
299         dest_buffer_ = newUnnamedFile(initpath, to_utf8(_("differences")));
300
301         if (!new_buffer_ || !old_buffer_ || !dest_buffer_)
302                 return 0;
303
304         dest_buffer_->changed(true);
305         dest_buffer_->markDirty();
306
307         // get the options from the dialog
308         CompareOptions options;
309         options.settings_from_new = newSettingsRB->isChecked();
310
311         // init the compare object and start it
312         compare_ = new Compare(new_buffer_, old_buffer_, dest_buffer_, options);
313         connect(compare_, SIGNAL(error()), this, SLOT(error()));
314         connect(compare_, SIGNAL(finished(bool)), this, SLOT(finished(bool)));
315         connect(compare_, SIGNAL(progress(int)), this, SLOT(progress(int)));
316         connect(compare_, SIGNAL(progressMax(int)), this, SLOT(progressMax(int)));
317         connect(compare_, SIGNAL(statusMessage(QString)),
318                 this, SLOT(setStatusMessage(QString)));
319         compare_->start(QThread::LowPriority);
320         return 1;
321 }
322
323 bool GuiCompare::initialiseParams(std::string const &par)
324 {
325         //just for the sake of parsing arguments
326         FuncRequest cmd(LFUN_UNKNOWN_ACTION, par);
327         if (cmd.getArg(0) == "run") {
328                 oldFileCB->setEditText(toqstr(cmd.getArg(1)));
329                 newFileCB->setEditText(toqstr(cmd.getArg(2)));
330                 slotOK();
331         }
332
333         progressBar->setValue(0);
334         progressBar->setEnabled(false);
335         progressBar->setMaximum(1);
336
337         return true;
338 }
339
340 Dialog * createGuiCompare(GuiView & lv) { return new GuiCompare(lv); }
341
342
343 } // namespace frontend
344 } // namespace lyx
345
346
347 #include "moc_GuiCompare.cpp"