]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiCompare.cpp
Add a boolean to GuiWorkArea::redraw to indicate whether the metrics must be updated...
[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         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         BufferList::iterator it = theBufferList().begin();
110         BufferList::iterator const end = theBufferList().end();
111         for (; it != end; ++it) {
112                 QString filename = toqstr((*it)->absFileName());
113                 newFileCB->addItem(filename);
114                 oldFileCB->addItem(filename);
115         }
116         if (lyxview().documentBufferView())
117                 newFileCB->setEditText(toqstr(buffer().absFileName()));
118         else
119                 newFileCB->setEditText(restore_filename1);
120
121         if (!restore_filename2.isEmpty())
122                 oldFileCB->setEditText(restore_filename2);
123         else
124                 oldFileCB->clearEditText();
125
126         if (isValid()) {
127                 bc().setValid(isValid());
128                 bc().apply();
129         }
130 }
131
132
133 void GuiCompare::selectNewFile()
134 {
135         QString name = browse(newFileCB->currentText());
136         if (!name.isEmpty())
137                 newFileCB->setEditText(name);
138         changed();
139 }
140
141
142 void GuiCompare::selectOldFile()
143 {
144         QString name = browse(oldFileCB->currentText());
145         if (!name.isEmpty())
146                 oldFileCB->setEditText(name);
147         changed();
148 }
149
150
151 QString GuiCompare::browse(QString const & in_name) const
152 {
153         QString const title = qt_("Select document");
154
155         QStringList const & filters = fileFilters(qt_("LyX Documents (*.lyx)"));
156         
157         QString filename;
158         if (lyxview().documentBufferView()) {
159                 QString path = bufferFilepath();
160                 filename = browseRelFile(in_name, path, title, filters, false, 
161                         qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
162         } else {
163                 QString path = toqstr(lyxrc.document_path);
164                 QString rel_filename = browseRelFile(in_name, path, title, filters, false, 
165                         qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
166                 filename = makeAbsPath(rel_filename, path);
167         }
168         return filename;        
169 }
170
171
172 void GuiCompare::enableControls(bool enable) const
173 {
174         newFileLA->setEnabled(enable);
175         newFilePB->setEnabled(enable);
176         newFileCB->setEnabled(enable);
177         oldFileLA->setEnabled(enable);
178         oldFilePB->setEnabled(enable);
179         oldFileCB->setEnabled(enable);
180         okPB->setEnabled(enable);
181         groupBox->setEnabled(enable);
182         progressBar->setEnabled(!enable);
183
184         if (enable)
185                 closePB->setText(qt_("Close"));
186         else
187                 closePB->setText(qt_("Cancel"));
188 }
189
190
191 void GuiCompare::error()
192 {
193         Alert::error(_("Error"), _("Error while comparing documents."));
194         window_title_ = windowTitle();
195         finished(true);
196 }
197
198 void GuiCompare::finished(bool aborted)
199 {
200         enableControls(true);
201
202         if (compare_) {
203                 delete compare_;
204                 compare_ = 0;
205         }
206         
207         if (aborted) {
208                 if (dest_buffer_) {
209                         dest_buffer_->markClean();
210                         theBufferList().release(dest_buffer_);
211                 }
212                 setWindowTitle(window_title_);
213                 progressBar->setValue(0);
214         } else {
215                 hideView();
216                 bc().ok();
217                 if (dest_buffer_) {
218                         dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
219                                 dest_buffer_->absFileName()));
220                 }
221         }
222 }
223
224
225 void GuiCompare::progress(int val)
226 {
227         progressBar->setValue(progressBar->value() + val);
228 }
229
230
231 void GuiCompare::progressMax(int max) const
232 {
233         progressBar->setMaximum(max);
234 }
235         
236
237 void GuiCompare::slotOK()
238 {
239         enableControls(false);
240         if (!run())
241                 error();
242 }
243
244
245 void GuiCompare::slotCancel()
246 {
247         if (compare_ && compare_->isRunning()) {
248                 window_title_ = windowTitle();
249                 setWindowTitle(window_title_ + " " + qt_("(cancelling)"));
250                 compare_->abort();
251         } else {
252                 GuiDialog::slotClose();
253                 progressBar->setValue(0);
254         }
255 }
256
257
258 Buffer const * GuiCompare::bufferFromFileName(string const & file) const
259 {
260         FileName fname;
261         if (FileName::isAbsolute(file))
262                 fname.set(file);
263         else if (lyxview().documentBufferView())
264                 fname = support::makeAbsPath(file, fromqstr(bufferFilepath()));
265
266         if (fname.empty()
267                         || (!fname.exists() && !theBufferList().getBuffer(fname))) {
268                 LYXERR0( "Unable to read: " << file);
269                 return 0;
270         }
271         return loadIfNeeded(fname);
272 }
273
274
275 int GuiCompare::run()
276 {
277         progressBar->setValue(0);
278
279         new_buffer_ = bufferFromFileName(fromqstr(newFileCB->currentText()));
280         old_buffer_ = bufferFromFileName(fromqstr(oldFileCB->currentText()));
281
282         // new buffer that will carry the output
283         FileName initpath(lyxrc.document_path);
284         dest_buffer_ = newUnnamedFile(initpath, to_utf8(_("differences")));
285
286         if (!new_buffer_ || !old_buffer_ || !dest_buffer_)
287                 return 0;
288
289         dest_buffer_->changed(true);
290         dest_buffer_->markDirty();
291
292         // get the options from the dialog
293         CompareOptions options;
294         options.settings_from_new = newSettingsRB->isChecked();
295
296         // init the compare object and start it
297         compare_ = new Compare(new_buffer_, old_buffer_, dest_buffer_, options);
298         connect(compare_, SIGNAL(error()), this, SLOT(error()));
299         connect(compare_, SIGNAL(finished(bool)), this, SLOT(finished(bool)));
300         connect(compare_, SIGNAL(progress(int)), this, SLOT(progress(int)));
301         connect(compare_, SIGNAL(progressMax(int)), this, SLOT(progressMax(int)));
302         compare_->start(QThread::LowPriority);
303         return 1;
304 }
305
306
307 Dialog * createGuiCompare(GuiView & lv) { return new GuiCompare(lv); }
308
309
310 } // namespace frontend
311 } // namespace lyx
312
313
314 #include "moc_GuiCompare.cpp"