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"
19 #include "FuncRequest.h"
22 #include "qt_helpers.h"
24 #include "frontends/alert.h"
26 #include "support/debug.h"
27 #include "support/filetools.h"
28 #include "support/FileName.h"
29 #include "support/gettext.h"
35 using namespace lyx::support;
41 GuiCompare::GuiCompare(GuiView & lv)
42 : GuiDialog(lv, "compare", qt_("Compare LyX files")),
43 compare_(0), dest_buffer_(0)
46 setModal(Qt::WindowModal);
48 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
49 connect(closePB, SIGNAL(clicked()), this, SLOT(slotCancel()));
51 connect(newFilePB, SIGNAL(clicked()), this, SLOT(select_newfile()));
52 connect(oldFilePB, SIGNAL(clicked()), this, SLOT(select_oldfile()));
54 connect(newFileCB, SIGNAL(currentIndexChanged(int)),
55 this, SLOT(change_adaptor()));
56 connect(newFileCB, SIGNAL(editTextChanged(const QString &)),
57 this, SLOT(change_adaptor()));
58 connect(oldFileCB, SIGNAL(currentIndexChanged(int)),
59 this, SLOT(change_adaptor()));
60 connect(oldFileCB, SIGNAL(editTextChanged(const QString &)),
61 this, SLOT(change_adaptor()));
63 newSettingsRB->setChecked(true);
65 progressBar->setValue(0);
66 progressBar->setEnabled(false);
68 bc().setPolicy(ButtonPolicy::OkApplyCancelPolicy);
72 GuiCompare::~GuiCompare()
76 void GuiCompare::closeEvent(QCloseEvent *)
82 void GuiCompare::change_adaptor()
88 bool GuiCompare::isValid()
90 bool const valid = !newFileCB->currentText().isEmpty()
91 && !oldFileCB->currentText().isEmpty();
96 void GuiCompare::updateContents()
98 QString restore_filename1 = newFileCB->currentText();
99 QString restore_filename2 = oldFileCB->currentText();
102 progressBar->setValue(0);
103 BufferList::iterator it = theBufferList().begin();
104 BufferList::iterator const end = theBufferList().end();
105 for (; it != end; ++it) {
106 QString filename = toqstr((*it)->absFileName());
107 newFileCB->addItem(filename);
108 oldFileCB->addItem(filename);
110 if (lyxview().documentBufferView())
111 newFileCB->setEditText(toqstr(buffer().absFileName()));
113 newFileCB->setEditText(restore_filename1);
115 if (!restore_filename2.isEmpty())
116 oldFileCB->setEditText(restore_filename2);
118 oldFileCB->clearEditText();
121 bc().setValid(isValid());
127 void GuiCompare::select_newfile()
129 QString name = browse(newFileCB->currentText());
131 newFileCB->setEditText(name);
136 void GuiCompare::select_oldfile()
138 QString name = browse(oldFileCB->currentText());
140 oldFileCB->setEditText(name);
145 QString GuiCompare::browse(QString const & in_name) const
147 QString const title = qt_("Select document");
149 QStringList const & filters = fileFilters(qt_("LyX Documents (*.lyx)"));
152 if (lyxview().documentBufferView()) {
153 QString path = bufferFilepath();
154 filename = browseRelFile(in_name, path, title, filters, false,
155 qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
157 QString path = toqstr(lyxrc.document_path);
158 QString rel_filename = browseRelFile(in_name, path, title, filters, false,
159 qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
160 filename = makeAbsPath(rel_filename, path);
166 void GuiCompare::enableControls(bool enable) const
168 newFileLA->setEnabled(enable);
169 newFilePB->setEnabled(enable);
170 newFileCB->setEnabled(enable);
171 oldFileLA->setEnabled(enable);
172 oldFilePB->setEnabled(enable);
173 oldFileCB->setEnabled(enable);
174 okPB->setEnabled(enable);
175 groupBox->setEnabled(enable);
176 progressBar->setEnabled(!enable);
179 closePB->setText(qt_("Close"));
181 closePB->setText(qt_("Cancel"));
185 void GuiCompare::finished(bool aborted)
187 enableControls(true);
190 dest_buffer_->markClean();
191 theBufferList().release(dest_buffer_);
192 setWindowTitle(window_title_);
193 progressBar->setValue(0);
197 dispatch(FuncRequest(LFUN_BUFFER_SWITCH, dest_buffer_->absFileName()));
202 void GuiCompare::nextIt(int val)
204 progressBar->setValue(progressBar->value() + val);
208 void GuiCompare::progress_max(int max) const
210 progressBar->setMaximum(max);
214 void GuiCompare::slotOK()
216 enableControls(false);
218 Alert::error(_("Error"),
219 _("Unable to compare files."));
225 void GuiCompare::slotCancel()
227 GuiDialog::slotClose();
228 progressBar->setValue(0);
232 Buffer const * GuiCompare::bufferFromFileName(string const & file) const
235 if (FileName::isAbsolute(file))
237 else if (lyxview().documentBufferView())
238 fname = support::makeAbsPath(file, fromqstr(bufferFilepath()));
241 || (!fname.exists() && !theBufferList().getBuffer(fname))) {
242 LYXERR0( "Unable to read: " << file);
245 return loadIfNeeded(fname);
249 int GuiCompare::run()
251 progressBar->setValue(0);
253 new_buffer_ = bufferFromFileName(fromqstr(newFileCB->currentText()));
254 old_buffer_ = bufferFromFileName(fromqstr(oldFileCB->currentText()));
256 // new buffer that will carry the output
257 FileName initpath(lyxrc.document_path);
258 dest_buffer_ = newUnnamedFile(initpath, to_utf8(_("differences")));
259 dest_buffer_->changed();
260 dest_buffer_->markDirty();
266 Dialog * createGuiCompare(GuiView & lv) { return new GuiCompare(lv); }
269 } // namespace frontend
273 #include "moc_GuiCompare.cpp"