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 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 (lyxview().documentBufferView())
117 newFileCB->setEditText(toqstr(buffer().absFileName()));
119 newFileCB->setEditText(restore_filename1);
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) const
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);
185 closePB->setText(qt_("Close"));
187 closePB->setText(qt_("Cancel"));
191 void GuiCompare::error()
193 Alert::error(_("Error"), _("Error while comparing documents."));
194 window_title_ = windowTitle();
198 void GuiCompare::finished(bool aborted)
200 enableControls(true);
209 dest_buffer_->markClean();
210 theBufferList().release(dest_buffer_);
212 setWindowTitle(window_title_);
213 progressBar->setValue(0);
218 dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
219 dest_buffer_->absFileName()));
225 void GuiCompare::progress(int val)
227 progressBar->setValue(progressBar->value() + val);
231 void GuiCompare::progressMax(int max) const
233 progressBar->setMaximum(max);
237 void GuiCompare::slotOK()
239 enableControls(false);
245 void GuiCompare::slotCancel()
247 if (compare_ && compare_->isRunning()) {
248 window_title_ = windowTitle();
249 setWindowTitle(window_title_ + " " + qt_("(cancelling)"));
252 GuiDialog::slotClose();
253 progressBar->setValue(0);
258 Buffer const * GuiCompare::bufferFromFileName(string const & file) const
261 if (FileName::isAbsolute(file))
263 else if (lyxview().documentBufferView())
264 fname = support::makeAbsPath(file, fromqstr(bufferFilepath()));
267 || (!fname.exists() && !theBufferList().getBuffer(fname))) {
268 LYXERR0( "Unable to read: " << file);
271 return loadIfNeeded(fname);
275 int GuiCompare::run()
277 progressBar->setValue(0);
279 new_buffer_ = bufferFromFileName(fromqstr(newFileCB->currentText()));
280 old_buffer_ = bufferFromFileName(fromqstr(oldFileCB->currentText()));
282 // new buffer that will carry the output
283 FileName initpath(lyxrc.document_path);
284 dest_buffer_ = newUnnamedFile(initpath, to_utf8(_("differences")));
286 if (!new_buffer_ || !old_buffer_ || !dest_buffer_)
289 dest_buffer_->changed(true);
290 dest_buffer_->markDirty();
292 // get the options from the dialog
293 CompareOptions options;
294 options.settings_from_new = newSettingsRB->isChecked();
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);
307 Dialog * createGuiCompare(GuiView & lv) { return new GuiCompare(lv); }
310 } // namespace frontend
314 #include "moc_GuiCompare.cpp"