]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiCompare.cpp
* fix spelling in comments to please John.
[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
66         progressBar->setValue(0);
67         progressBar->setEnabled(false);
68
69         closePB->setCursor(Qt::ArrowCursor);
70
71         bc().setPolicy(ButtonPolicy::OkApplyCancelPolicy);
72         bc().setOK(okPB);
73 }
74
75 GuiCompare::~GuiCompare()
76 {
77         if (compare_)
78                 delete compare_;
79 }
80
81 void GuiCompare::closeEvent(QCloseEvent *)
82 {
83         slotCancel();   
84 }
85
86
87 void GuiCompare::changeAdaptor()
88 {
89         changed();
90 }
91
92
93 bool GuiCompare::isValid()
94 {
95         bool const valid = !newFileCB->currentText().isEmpty()
96                 && !oldFileCB->currentText().isEmpty();
97         return valid;
98 }
99
100
101 void GuiCompare::updateContents()
102 {
103         if (compare_ && compare_->isRunning())
104                 return;
105
106         QString restore_filename1 = newFileCB->currentText();
107         QString restore_filename2 = oldFileCB->currentText();
108         newFileCB->clear();
109         oldFileCB->clear();
110         progressBar->setValue(0);
111         statusBar->clearMessage();
112         BufferList::iterator it = theBufferList().begin();
113         BufferList::iterator const end = theBufferList().end();
114         for (; it != end; ++it) {
115                 QString filename = toqstr((*it)->absFileName());
116                 newFileCB->addItem(filename);
117                 oldFileCB->addItem(filename);
118         }
119         if (lyxview().documentBufferView())
120                 newFileCB->setEditText(toqstr(buffer().absFileName()));
121         else
122                 newFileCB->setEditText(restore_filename1);
123
124         if (!restore_filename2.isEmpty())
125                 oldFileCB->setEditText(restore_filename2);
126         else
127                 oldFileCB->clearEditText();
128
129         if (isValid()) {
130                 bc().setValid(isValid());
131                 bc().apply();
132         }
133 }
134
135
136 void GuiCompare::selectNewFile()
137 {
138         QString name = browse(newFileCB->currentText());
139         if (!name.isEmpty())
140                 newFileCB->setEditText(name);
141         changed();
142 }
143
144
145 void GuiCompare::selectOldFile()
146 {
147         QString name = browse(oldFileCB->currentText());
148         if (!name.isEmpty())
149                 oldFileCB->setEditText(name);
150         changed();
151 }
152
153
154 QString GuiCompare::browse(QString const & in_name) const
155 {
156         QString const title = qt_("Select document");
157
158         QStringList const & filters = fileFilters(qt_("LyX Documents (*.lyx)"));
159         
160         QString filename;
161         if (lyxview().documentBufferView()) {
162                 QString path = bufferFilepath();
163                 filename = browseRelFile(in_name, path, title, filters, false, 
164                         qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
165         } else {
166                 QString path = toqstr(lyxrc.document_path);
167                 QString rel_filename = browseRelFile(in_name, path, title, filters, false, 
168                         qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
169                 filename = makeAbsPath(rel_filename, path);
170         }
171         return filename;        
172 }
173
174
175 void GuiCompare::enableControls(bool enable)
176 {
177         // Set the hourglass cursor for the dialog, but
178         // never for the cancel button.
179         setCursor(enable ? Qt::ArrowCursor : Qt::WaitCursor);
180
181         newFileLA->setEnabled(enable);
182         newFilePB->setEnabled(enable);
183         newFileCB->setEnabled(enable);
184         oldFileLA->setEnabled(enable);
185         oldFilePB->setEnabled(enable);
186         oldFileCB->setEnabled(enable);
187         okPB->setEnabled(enable);
188         groupBox->setEnabled(enable);
189         progressBar->setEnabled(!enable);
190
191         if (enable)
192                 closePB->setText(qt_("Close"));
193         else
194                 closePB->setText(qt_("Cancel"));
195 }
196
197
198 void GuiCompare::error()
199 {
200         Alert::error(_("Error"), _("Error while comparing documents."));
201         finished(true);
202 }
203
204 void GuiCompare::finished(bool aborted)
205 {
206         enableControls(true);
207
208         if (compare_) {
209                 delete compare_;
210                 compare_ = 0;
211         }
212         
213         if (aborted) {
214                 if (dest_buffer_) {
215                         dest_buffer_->markClean();
216                         theBufferList().release(dest_buffer_);
217                 }
218                 progressBar->setValue(0);
219                 statusBar->showMessage(qt_("Aborted"), 5000);
220         } else {
221                 hideView();
222                 bc().ok();
223                 if (dest_buffer_) {
224                         dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
225                                 dest_buffer_->absFileName()));
226                 }
227                 statusBar->showMessage(qt_("Finished"), 5000);
228         }
229 }
230
231
232 void GuiCompare::progress(int val)
233 {
234         progressBar->setValue(progressBar->value() + val);
235 }
236
237
238 void GuiCompare::progressMax(int max) const
239 {
240         progressBar->setMaximum(max);
241 }
242
243
244 void GuiCompare::setStatusMessage(QString msg)
245 {
246         statusBar->showMessage(msg);
247 }
248
249
250 void GuiCompare::slotOK()
251 {
252         enableControls(false);
253         if (!run())
254                 error();
255 }
256
257
258 void GuiCompare::slotCancel()
259 {
260         if (compare_ && compare_->isRunning()) {
261                 statusBar->showMessage(qt_("Aborting process..."));
262                 compare_->abort();
263         } else {
264                 GuiDialog::slotClose();
265                 progressBar->setValue(0);
266                 statusBar->clearMessage();
267         }
268 }
269
270
271 Buffer const * GuiCompare::bufferFromFileName(string const & file) const
272 {
273         FileName fname;
274         if (FileName::isAbsolute(file))
275                 fname.set(file);
276         else if (lyxview().documentBufferView())
277                 fname = support::makeAbsPath(file, fromqstr(bufferFilepath()));
278
279         if (fname.empty()
280                         || (!fname.exists() && !theBufferList().getBuffer(fname))) {
281                 LYXERR0( "Unable to read: " << file);
282                 return 0;
283         }
284         return loadIfNeeded(fname);
285 }
286
287
288 int GuiCompare::run()
289 {
290         progressBar->setValue(0);
291
292         new_buffer_ = bufferFromFileName(fromqstr(newFileCB->currentText()));
293         old_buffer_ = bufferFromFileName(fromqstr(oldFileCB->currentText()));
294
295         // new buffer that will carry the output
296         FileName initpath(lyxrc.document_path);
297         dest_buffer_ = newUnnamedFile(initpath, to_utf8(_("differences")));
298
299         if (!new_buffer_ || !old_buffer_ || !dest_buffer_)
300                 return 0;
301
302         dest_buffer_->changed(true);
303         dest_buffer_->markDirty();
304
305         // get the options from the dialog
306         CompareOptions options;
307         options.settings_from_new = newSettingsRB->isChecked();
308
309         // init the compare object and start it
310         compare_ = new Compare(new_buffer_, old_buffer_, dest_buffer_, options);
311         connect(compare_, SIGNAL(error()), this, SLOT(error()));
312         connect(compare_, SIGNAL(finished(bool)), this, SLOT(finished(bool)));
313         connect(compare_, SIGNAL(progress(int)), this, SLOT(progress(int)));
314         connect(compare_, SIGNAL(progressMax(int)), this, SLOT(progressMax(int)));
315         connect(compare_, SIGNAL(statusMessage(QString)),
316                 this, SLOT(setStatusMessage(QString)));
317         compare_->start(QThread::LowPriority);
318         return 1;
319 }
320
321
322 Dialog * createGuiCompare(GuiView & lv) { return new GuiCompare(lv); }
323
324
325 } // namespace frontend
326 } // namespace lyx
327
328
329 #include "moc_GuiCompare.cpp"