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 bc().setPolicy(ButtonPolicy::OkApplyCancelPolicy);
73 GuiCompare::~GuiCompare()
79 void GuiCompare::closeEvent(QCloseEvent *)
85 void GuiCompare::changeAdaptor()
91 bool GuiCompare::isValid()
93 bool const valid = !newFileCB->currentText().isEmpty()
94 && !oldFileCB->currentText().isEmpty();
99 void GuiCompare::updateContents()
101 if (compare_ && compare_->isRunning())
104 QString restore_filename1 = newFileCB->currentText();
105 QString restore_filename2 = oldFileCB->currentText();
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);
117 if (lyxview().documentBufferView())
118 newFileCB->setEditText(toqstr(buffer().absFileName()));
120 newFileCB->setEditText(restore_filename1);
122 if (!restore_filename2.isEmpty())
123 oldFileCB->setEditText(restore_filename2);
125 oldFileCB->clearEditText();
128 bc().setValid(isValid());
134 void GuiCompare::selectNewFile()
136 QString name = browse(newFileCB->currentText());
138 newFileCB->setEditText(name);
143 void GuiCompare::selectOldFile()
145 QString name = browse(oldFileCB->currentText());
147 oldFileCB->setEditText(name);
152 QString GuiCompare::browse(QString const & in_name) const
154 QString const title = qt_("Select document");
156 QStringList const & filters = fileFilters(qt_("LyX Documents (*.lyx)"));
159 if (lyxview().documentBufferView()) {
160 QString path = bufferFilepath();
161 filename = browseRelFile(in_name, path, title, filters, false,
162 qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
164 QString path = toqstr(lyxrc.document_path);
165 QString rel_filename = browseRelFile(in_name, path, title, filters, false,
166 qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
167 filename = makeAbsPath(rel_filename, path);
173 void GuiCompare::enableControls(bool enable) const
175 newFileLA->setEnabled(enable);
176 newFilePB->setEnabled(enable);
177 newFileCB->setEnabled(enable);
178 oldFileLA->setEnabled(enable);
179 oldFilePB->setEnabled(enable);
180 oldFileCB->setEnabled(enable);
181 okPB->setEnabled(enable);
182 groupBox->setEnabled(enable);
183 progressBar->setEnabled(!enable);
186 closePB->setText(qt_("Close"));
188 closePB->setText(qt_("Cancel"));
192 void GuiCompare::error()
194 Alert::error(_("Error"), _("Error while comparing documents."));
195 window_title_ = windowTitle();
199 void GuiCompare::finished(bool aborted)
201 enableControls(true);
210 dest_buffer_->markClean();
211 theBufferList().release(dest_buffer_);
213 setWindowTitle(window_title_);
214 progressBar->setValue(0);
215 statusBar->showMessage(qt_("Aborted"));
220 dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
221 dest_buffer_->absFileName()));
223 statusBar->showMessage(qt_("Finished"));
228 void GuiCompare::progress(int val)
230 progressBar->setValue(progressBar->value() + val);
234 void GuiCompare::progressMax(int max) const
236 progressBar->setMaximum(max);
241 void GuiCompare::setStatusMessage(QString msg)
243 statusBar->showMessage(msg);
247 void GuiCompare::slotOK()
249 enableControls(false);
255 void GuiCompare::slotCancel()
257 if (compare_ && compare_->isRunning()) {
258 window_title_ = windowTitle();
259 setWindowTitle(window_title_ + " " + qt_("(cancelling)"));
260 statusBar->showMessage(qt_("Aborting process..."));
263 GuiDialog::slotClose();
264 progressBar->setValue(0);
265 statusBar->clearMessage();
270 Buffer const * GuiCompare::bufferFromFileName(string const & file) const
273 if (FileName::isAbsolute(file))
275 else if (lyxview().documentBufferView())
276 fname = support::makeAbsPath(file, fromqstr(bufferFilepath()));
279 || (!fname.exists() && !theBufferList().getBuffer(fname))) {
280 LYXERR0( "Unable to read: " << file);
283 return loadIfNeeded(fname);
287 int GuiCompare::run()
289 progressBar->setValue(0);
291 new_buffer_ = bufferFromFileName(fromqstr(newFileCB->currentText()));
292 old_buffer_ = bufferFromFileName(fromqstr(oldFileCB->currentText()));
294 // new buffer that will carry the output
295 FileName initpath(lyxrc.document_path);
296 dest_buffer_ = newUnnamedFile(initpath, to_utf8(_("differences")));
298 if (!new_buffer_ || !old_buffer_ || !dest_buffer_)
301 dest_buffer_->changed(true);
302 dest_buffer_->markDirty();
304 // get the options from the dialog
305 CompareOptions options;
306 options.settings_from_new = newSettingsRB->isChecked();
308 // init the compare object and start it
309 compare_ = new Compare(new_buffer_, old_buffer_, dest_buffer_, options);
310 connect(compare_, SIGNAL(error()), this, SLOT(error()));
311 connect(compare_, SIGNAL(finished(bool)), this, SLOT(finished(bool)));
312 connect(compare_, SIGNAL(progress(int)), this, SLOT(progress(int)));
313 connect(compare_, SIGNAL(progressMax(int)), this, SLOT(progressMax(int)));
314 connect(compare_, SIGNAL(statusMessage(QString)),
315 this, SLOT(setStatusMessage(QString)));
316 compare_->start(QThread::LowPriority);
321 Dialog * createGuiCompare(GuiView & lv) { return new GuiCompare(lv); }
324 } // namespace frontend
328 #include "moc_GuiCompare.cpp"