]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiCompare.cpp
Make trunk compile for now.
[lyx.git] / src / frontends / qt4 / GuiCompare.cpp
1 /**
2  * \file GuiCompare.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Vincent van Ravesteijn
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "GuiCompare.h"
14
15 #include "Buffer.h"
16 #include "BufferView.h"
17 #include "BufferList.h"
18 #include "buffer_funcs.h"
19 #include "FuncRequest.h"
20 #include "GuiView.h"
21 #include "LyXRC.h"
22 #include "qt_helpers.h"
23
24 #include "frontends/alert.h"
25
26 #include "support/debug.h"
27 #include "support/filetools.h"
28 #include "support/FileName.h"
29 #include "support/gettext.h"
30
31 #include <QThread>
32
33
34 using namespace std;
35 using namespace lyx::support;
36
37 namespace lyx {
38 namespace frontend {
39
40
41 GuiCompare::GuiCompare(GuiView & lv)
42         : GuiDialog(lv, "compare", qt_("Compare LyX files")),
43         dest_buffer_(0)
44 {
45         setupUi(this);
46         setModal(Qt::WindowModal);
47
48         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
49         connect(closePB, SIGNAL(clicked()), this, SLOT(slotCancel()));
50
51         connect(newFilePB, SIGNAL(clicked()), this, SLOT(select_newfile()));
52         connect(oldFilePB, SIGNAL(clicked()), this, SLOT(select_oldfile()));
53
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()));
62
63         newSettingsRB->setChecked(true);
64
65         progressBar->setValue(0);
66         progressBar->setEnabled(false);
67
68         bc().setPolicy(ButtonPolicy::OkApplyCancelPolicy);
69         bc().setOK(okPB);
70 }
71
72 GuiCompare::~GuiCompare()
73 {
74 }
75
76 void GuiCompare::closeEvent(QCloseEvent *)
77 {
78         slotCancel();   
79 }
80
81
82 void GuiCompare::change_adaptor()
83 {
84         changed();
85 }
86
87
88 bool GuiCompare::isValid()
89 {
90         bool const valid = !newFileCB->currentText().isEmpty()
91                 && !oldFileCB->currentText().isEmpty();
92         return valid;
93 }
94
95
96 void GuiCompare::updateContents()
97 {
98         QString restore_filename1 = newFileCB->currentText();
99         QString restore_filename2 = oldFileCB->currentText();
100         newFileCB->clear();
101         oldFileCB->clear();
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);
109         }
110         if (lyxview().documentBufferView())
111                 newFileCB->setEditText(toqstr(buffer().absFileName()));
112         else
113                 newFileCB->setEditText(restore_filename1);
114
115         if (!restore_filename2.isEmpty())
116                 oldFileCB->setEditText(restore_filename2);
117         else
118                 oldFileCB->clearEditText();
119
120         if (isValid()) {
121                 bc().setValid(isValid());
122                 bc().apply();
123         }
124 }
125
126
127 void GuiCompare::select_newfile()
128 {
129         QString name = browse(newFileCB->currentText());
130         if (!name.isEmpty())
131                 newFileCB->setEditText(name);
132         changed();
133 }
134
135
136 void GuiCompare::select_oldfile()
137 {
138         QString name = browse(oldFileCB->currentText());
139         if (!name.isEmpty())
140                 oldFileCB->setEditText(name);
141         changed();
142 }
143
144
145 QString GuiCompare::browse(QString const & in_name) const
146 {
147         QString const title = qt_("Select document");
148
149         QStringList const & filters = fileFilters(qt_("LyX Documents (*.lyx)"));
150         
151         QString filename;
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));
156         } else {
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);
161         }
162         return filename;        
163 }
164
165
166 void GuiCompare::enableControls(bool enable) const
167 {
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);
177
178         if (enable)
179                 closePB->setText(qt_("Close"));
180         else
181                 closePB->setText(qt_("Cancel"));
182 }
183
184
185 void GuiCompare::finished(bool aborted)
186 {
187         enableControls(true);
188         
189         if (aborted) {
190                 dest_buffer_->markClean();
191                 theBufferList().release(dest_buffer_);
192                 setWindowTitle(window_title_);
193                 progressBar->setValue(0);
194         } else {
195                 hideView();
196                 bc().ok();
197                 dispatch(FuncRequest(LFUN_BUFFER_SWITCH, dest_buffer_->absFileName()));
198         }
199 }
200
201
202 void GuiCompare::nextIt(int val)
203 {
204         progressBar->setValue(progressBar->value() + val);
205 }
206
207
208 void GuiCompare::progress_max(int max) const
209 {
210         progressBar->setMaximum(max);
211 }
212         
213
214 void GuiCompare::slotOK()
215 {
216         enableControls(false);
217         if (!run()) {
218                 Alert::error(_("Error"),
219                         _("Unable to compare files."));
220                 finished(true);
221         }
222 }
223
224
225 void GuiCompare::slotCancel()
226 {
227         GuiDialog::slotClose();
228         progressBar->setValue(0);
229 }
230
231
232 Buffer const * GuiCompare::bufferFromFileName(string const & file) const
233 {
234         FileName fname;
235         if (FileName::isAbsolute(file))
236                 fname.set(file);
237         else if (lyxview().documentBufferView())
238                 fname = support::makeAbsPath(file, fromqstr(bufferFilepath()));
239
240         if (fname.empty()
241                         || (!fname.exists() && !theBufferList().getBuffer(fname))) {
242                 LYXERR0( "Unable to read: " << file);
243                 return 0;
244         }
245         return loadIfNeeded(fname);
246 }
247
248
249 int GuiCompare::run()
250 {
251         progressBar->setValue(0);
252
253         new_buffer_ = bufferFromFileName(fromqstr(newFileCB->currentText()));
254         old_buffer_ = bufferFromFileName(fromqstr(oldFileCB->currentText()));
255
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();
261
262         return 0;
263 }
264
265
266 Dialog * createGuiCompare(GuiView & lv) { return new GuiCompare(lv); }
267
268
269 } // namespace frontend
270 } // namespace lyx
271
272
273 #include "moc_GuiCompare.cpp"