3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Vincent van Ravesteijn
8 * Full author contact details are available in file CREDITS.
13 #include "GuiCompare.h"
16 #include "BufferView.h"
17 #include "BufferList.h"
18 #include "buffer_funcs.h"
20 #include "FuncRequest.h"
23 #include "qt_helpers.h"
25 #include "frontends/alert.h"
27 #include "support/debug.h"
28 #include "support/filetools.h"
29 #include "support/FileName.h"
30 #include "support/gettext.h"
36 using namespace lyx::support;
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)
47 setModal(Qt::WindowModal);
49 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
50 connect(closePB, SIGNAL(clicked()), this, SLOT(slotCancel()));
52 connect(newFilePB, SIGNAL(clicked()), this, SLOT(selectNewFile()));
53 connect(oldFilePB, SIGNAL(clicked()), this, SLOT(selectOldFile()));
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()));
64 newSettingsRB->setChecked(true);
66 progressBar->setValue(0);
67 progressBar->setEnabled(false);
69 closePB->setCursor(Qt::ArrowCursor);
71 bc().setPolicy(ButtonPolicy::OkApplyCancelPolicy);
75 GuiCompare::~GuiCompare()
81 void GuiCompare::closeEvent(QCloseEvent *)
87 void GuiCompare::changeAdaptor()
93 bool GuiCompare::isValid()
95 bool const valid = !newFileCB->currentText().isEmpty()
96 && !oldFileCB->currentText().isEmpty();
101 void GuiCompare::updateContents()
103 if (compare_ && compare_->isRunning())
106 QString restore_filename1 = newFileCB->currentText();
107 QString restore_filename2 = oldFileCB->currentText();
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);
119 if (lyxview().documentBufferView())
120 newFileCB->setEditText(toqstr(buffer().absFileName()));
122 newFileCB->setEditText(restore_filename1);
124 if (!restore_filename2.isEmpty())
125 oldFileCB->setEditText(restore_filename2);
127 oldFileCB->clearEditText();
130 bc().setValid(isValid());
136 void GuiCompare::selectNewFile()
138 QString name = browse(newFileCB->currentText());
140 newFileCB->setEditText(name);
145 void GuiCompare::selectOldFile()
147 QString name = browse(oldFileCB->currentText());
149 oldFileCB->setEditText(name);
154 QString GuiCompare::browse(QString const & in_name) const
156 QString const title = qt_("Select document");
158 QStringList const & filters = fileFilters(qt_("LyX Documents (*.lyx)"));
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));
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);
175 void GuiCompare::enableControls(bool enable)
177 // Set the hourglass cursor for the dialog, but
178 // never for the cancel button.
179 setCursor(enable ? Qt::ArrowCursor : Qt::WaitCursor);
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);
192 closePB->setText(qt_("Close"));
194 closePB->setText(qt_("Cancel"));
198 void GuiCompare::error()
200 Alert::error(_("Error"), _("Error while comparing documents."));
204 void GuiCompare::finished(bool aborted)
206 enableControls(true);
215 dest_buffer_->markClean();
216 theBufferList().release(dest_buffer_);
218 progressBar->setValue(0);
219 statusBar->showMessage(qt_("Aborted"), 5000);
224 dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
225 dest_buffer_->absFileName()));
227 statusBar->showMessage(qt_("Finished"), 5000);
232 void GuiCompare::progress(int val)
234 progressBar->setValue(progressBar->value() + val);
238 void GuiCompare::progressMax(int max) const
240 progressBar->setMaximum(max);
244 void GuiCompare::setStatusMessage(QString msg)
246 statusBar->showMessage(msg);
250 void GuiCompare::slotOK()
252 enableControls(false);
258 void GuiCompare::slotCancel()
260 if (compare_ && compare_->isRunning()) {
261 statusBar->showMessage(qt_("Aborting process..."));
264 GuiDialog::slotClose();
265 progressBar->setValue(0);
266 statusBar->clearMessage();
271 Buffer const * GuiCompare::bufferFromFileName(string const & file) const
274 if (FileName::isAbsolute(file))
276 else if (lyxview().documentBufferView())
277 fname = support::makeAbsPath(file, fromqstr(bufferFilePath()));
280 || (!fname.exists() && !theBufferList().getBuffer(fname))) {
281 LYXERR0( "Unable to read: " << file);
284 return loadIfNeeded(fname);
288 int GuiCompare::run()
290 progressBar->setValue(0);
292 new_buffer_ = bufferFromFileName(fromqstr(newFileCB->currentText()));
293 old_buffer_ = bufferFromFileName(fromqstr(oldFileCB->currentText()));
295 // new buffer that will carry the output
296 FileName initpath(lyxrc.document_path);
297 dest_buffer_ = newUnnamedFile(initpath, to_utf8(_("differences")));
299 if (!new_buffer_ || !old_buffer_ || !dest_buffer_)
302 dest_buffer_->changed(true);
303 dest_buffer_->markDirty();
305 // get the options from the dialog
306 CompareOptions options;
307 options.settings_from_new = newSettingsRB->isChecked();
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);
322 Dialog * createGuiCompare(GuiView & lv) { return new GuiCompare(lv); }
325 } // namespace frontend
329 #include "moc_GuiCompare.cpp"