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