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 closePB->setCursor(Qt::ArrowCursor);
68 bc().setPolicy(ButtonPolicy::OkApplyCancelPolicy);
72 GuiCompare::~GuiCompare()
78 void GuiCompare::closeEvent(QCloseEvent *)
84 void GuiCompare::changeAdaptor()
90 bool GuiCompare::isValid()
92 bool const valid = !newFileCB->currentText().isEmpty()
93 && !oldFileCB->currentText().isEmpty();
98 void GuiCompare::updateContents()
100 if (compare_ && compare_->isRunning())
103 QString restore_filename1 = newFileCB->currentText();
104 QString restore_filename2 = oldFileCB->currentText();
107 progressBar->setValue(0);
108 statusBar->clearMessage();
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);
116 if (!restore_filename1.isEmpty())
117 newFileCB->setEditText(restore_filename1);
118 else if (lyxview().documentBufferView())
119 newFileCB->setEditText(toqstr(buffer().absFileName()));
121 if (!restore_filename2.isEmpty())
122 oldFileCB->setEditText(restore_filename2);
124 oldFileCB->clearEditText();
127 bc().setValid(isValid());
133 void GuiCompare::selectNewFile()
135 QString name = browse(newFileCB->currentText());
137 newFileCB->setEditText(name);
142 void GuiCompare::selectOldFile()
144 QString name = browse(oldFileCB->currentText());
146 oldFileCB->setEditText(name);
151 QString GuiCompare::browse(QString const & in_name) const
153 QString const title = qt_("Select document");
155 QStringList const & filters = fileFilters(qt_("LyX Documents (*.lyx)"));
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));
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);
172 void GuiCompare::enableControls(bool enable)
174 // Set the hourglass cursor for the dialog, but
175 // never for the cancel button.
176 setCursor(enable ? Qt::ArrowCursor : Qt::WaitCursor);
178 newFileLA->setEnabled(enable);
179 newFilePB->setEnabled(enable);
180 newFileCB->setEnabled(enable);
181 oldFileLA->setEnabled(enable);
182 oldFilePB->setEnabled(enable);
183 oldFileCB->setEnabled(enable);
184 okPB->setEnabled(enable);
185 groupBox->setEnabled(enable);
186 progressBar->setEnabled(!enable);
189 closePB->setText(qt_("Close"));
191 closePB->setText(qt_("Cancel"));
195 void GuiCompare::error()
197 Alert::error(_("Error"), _("Error while comparing documents."));
201 void GuiCompare::finished(bool aborted)
203 enableControls(true);
212 dest_buffer_->markClean();
213 theBufferList().release(dest_buffer_);
215 progressBar->setValue(0);
216 statusBar->showMessage(qt_("Aborted"), 5000);
221 dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
222 dest_buffer_->absFileName()));
224 statusBar->showMessage(qt_("Finished"), 5000);
229 void GuiCompare::progress(int val)
231 progressBar->setValue(progressBar->value() + val);
235 void GuiCompare::progressMax(int max) const
237 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 statusBar->showMessage(qt_("Aborting process..."));
261 GuiDialog::slotClose();
262 progressBar->setValue(0);
263 statusBar->clearMessage();
268 Buffer const * GuiCompare::bufferFromFileName(string const & file) const
271 if (FileName::isAbsolute(file))
273 else if (lyxview().documentBufferView())
274 fname = support::makeAbsPath(file, fromqstr(bufferFilePath()));
277 || (!fname.exists() && !theBufferList().getBuffer(fname))) {
278 LYXERR0( "Unable to read: " << file);
281 return loadIfNeeded(fname);
285 int GuiCompare::run()
287 progressBar->setValue(0);
289 new_buffer_ = bufferFromFileName(fromqstr(newFileCB->currentText()));
290 old_buffer_ = bufferFromFileName(fromqstr(oldFileCB->currentText()));
292 // new buffer that will carry the output
293 FileName initpath(lyxrc.document_path);
294 dest_buffer_ = newUnnamedFile(initpath, to_utf8(_("differences")));
296 if (!new_buffer_ || !old_buffer_ || !dest_buffer_)
299 dest_buffer_->changed(true);
300 dest_buffer_->markDirty();
302 // get the options from the dialog
303 CompareOptions options;
304 options.settings_from_new = newSettingsRB->isChecked();
306 // init the compare object and start it
307 compare_ = new Compare(new_buffer_, old_buffer_, dest_buffer_, options);
308 connect(compare_, SIGNAL(error()), this, SLOT(error()));
309 connect(compare_, SIGNAL(finished(bool)), this, SLOT(finished(bool)));
310 connect(compare_, SIGNAL(progress(int)), this, SLOT(progress(int)));
311 connect(compare_, SIGNAL(progressMax(int)), this, SLOT(progressMax(int)));
312 connect(compare_, SIGNAL(statusMessage(QString)),
313 this, SLOT(setStatusMessage(QString)));
314 compare_->start(QThread::LowPriority);
318 bool GuiCompare::initialiseParams(std::string const &par)
320 //just for the sake of parsing arguments
321 FuncRequest cmd(LFUN_UNKNOWN_ACTION, par);
322 if (cmd.getArg(0) == "run") {
323 oldFileCB->setEditText(toqstr(cmd.getArg(1)));
324 newFileCB->setEditText(toqstr(cmd.getArg(2)));
328 progressBar->setValue(0);
329 progressBar->setEnabled(false);
330 progressBar->setMaximum(100);
335 Dialog * createGuiCompare(GuiView & lv) { return new GuiCompare(lv); }
338 } // namespace frontend
342 #include "moc_GuiCompare.cpp"