Call the Compare thread from the GuiCompare dialog.

Furthermore:
- increase safety, 
- improve error handling,
- minor cleanups,
- set documents to read-only while running the thread.


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@31736 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Vincent van Ravesteijn 2009-10-25 13:41:46 +00:00
parent 18483e6362
commit 7227566e86
6 changed files with 84 additions and 22 deletions

View File

@ -524,7 +524,7 @@ string Buffer::logName(LogType * type) const
} }
void Buffer::setReadonly(bool const flag) void Buffer::setReadonly(bool const flag) const
{ {
if (d->read_only != flag) { if (d->read_only != flag) {
d->read_only = flag; d->read_only = flag;

View File

@ -318,7 +318,7 @@ public:
bool isReadonly() const; bool isReadonly() const;
/// Set buffer read-only flag /// Set buffer read-only flag
void setReadonly(bool flag = true); void setReadonly(bool flag = true) const;
/// returns \c true if the buffer contains a LaTeX document /// returns \c true if the buffer contains a LaTeX document
bool isLatex() const; bool isLatex() const;

View File

@ -55,8 +55,10 @@ Compare::Compare(Buffer const * new_buf, Buffer const * old_buf,
void Compare::run() void Compare::run()
{ {
if (!dest_buffer || !new_buffer || !old_buffer) if (!dest_buffer || !new_buffer || !old_buffer) {
error();
return; return;
}
// Copy the buffer params to the new buffer // Copy the buffer params to the new buffer
dest_buffer->params() = options_.settings_from_new dest_buffer->params() = options_.settings_from_new
@ -64,8 +66,8 @@ void Compare::run()
// do the real work // do the real work
if (!doCompare()) if (!doCompare())
return; error();
else
finished(pimpl_->abort_); finished(pimpl_->abort_);
return; return;
} }
@ -82,7 +84,7 @@ void Compare::abort()
int Compare::doCompare() int Compare::doCompare()
{ {
return 1; return 0;
} }

View File

@ -60,8 +60,11 @@ public:
} }
Q_SIGNALS: Q_SIGNALS:
/// The thread has finished due to an error.
void error() const;
/// The thread has finished. If the thread is cancelled /// The thread has finished. If the thread is cancelled
/// by the user or due to an error \c aborted is true. /// by the user \c aborted is true.
void finished(bool aborted) const; void finished(bool aborted) const;
/// Adds \c progress to the value of the progress bar in the dialog /// Adds \c progress to the value of the progress bar in the dialog

View File

@ -16,6 +16,7 @@
#include "BufferView.h" #include "BufferView.h"
#include "BufferList.h" #include "BufferList.h"
#include "buffer_funcs.h" #include "buffer_funcs.h"
#include "Compare.h"
#include "FuncRequest.h" #include "FuncRequest.h"
#include "GuiView.h" #include "GuiView.h"
#include "LyXRC.h" #include "LyXRC.h"
@ -40,7 +41,7 @@ namespace frontend {
GuiCompare::GuiCompare(GuiView & lv) GuiCompare::GuiCompare(GuiView & lv)
: GuiDialog(lv, "compare", qt_("Compare LyX files")), : GuiDialog(lv, "compare", qt_("Compare LyX files")),
dest_buffer_(0) compare_(0), dest_buffer_(0), old_buffer_(0), new_buffer_(0)
{ {
setupUi(this); setupUi(this);
setModal(Qt::WindowModal); setModal(Qt::WindowModal);
@ -71,6 +72,8 @@ GuiCompare::GuiCompare(GuiView & lv)
GuiCompare::~GuiCompare() GuiCompare::~GuiCompare()
{ {
if (compare_)
delete compare_;
} }
void GuiCompare::closeEvent(QCloseEvent *) void GuiCompare::closeEvent(QCloseEvent *)
@ -95,6 +98,9 @@ bool GuiCompare::isValid()
void GuiCompare::updateContents() void GuiCompare::updateContents()
{ {
if (compare_ && compare_->isRunning())
return;
QString restore_filename1 = newFileCB->currentText(); QString restore_filename1 = newFileCB->currentText();
QString restore_filename2 = oldFileCB->currentText(); QString restore_filename2 = oldFileCB->currentText();
newFileCB->clear(); newFileCB->clear();
@ -182,24 +188,45 @@ void GuiCompare::enableControls(bool enable) const
} }
void GuiCompare::error()
{
Alert::error(_("Error"), _("Error while comparing documents."));
window_title_ = windowTitle();
finished(true);
}
void GuiCompare::finished(bool aborted) void GuiCompare::finished(bool aborted)
{ {
enableControls(true); enableControls(true);
if (old_buffer_)
old_buffer_->setReadonly(false);
if (new_buffer_)
new_buffer_->setReadonly(false);
if (compare_) {
delete compare_;
compare_ = 0;
}
if (aborted) { if (aborted) {
if (dest_buffer_) {
dest_buffer_->markClean(); dest_buffer_->markClean();
theBufferList().release(dest_buffer_); theBufferList().release(dest_buffer_);
}
setWindowTitle(window_title_); setWindowTitle(window_title_);
progressBar->setValue(0); progressBar->setValue(0);
} else { } else {
hideView(); hideView();
bc().ok(); bc().ok();
dispatch(FuncRequest(LFUN_BUFFER_SWITCH, dest_buffer_->absFileName())); if (dest_buffer_) {
dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
dest_buffer_->absFileName()));
}
} }
} }
void GuiCompare::nextIt(int val) void GuiCompare::progress(int val)
{ {
progressBar->setValue(progressBar->value() + val); progressBar->setValue(progressBar->value() + val);
} }
@ -214,19 +241,22 @@ void GuiCompare::progressMax(int max) const
void GuiCompare::slotOK() void GuiCompare::slotOK()
{ {
enableControls(false); enableControls(false);
if (!run()) { if (!run())
Alert::error(_("Error"), error();
_("Unable to compare files."));
finished(true);
}
} }
void GuiCompare::slotCancel() void GuiCompare::slotCancel()
{ {
if (compare_ && compare_->isRunning()) {
window_title_ = windowTitle();
setWindowTitle(window_title_ + " " + qt_("(cancelling)"));
compare_->abort();
} else {
GuiDialog::slotClose(); GuiDialog::slotClose();
progressBar->setValue(0); progressBar->setValue(0);
} }
}
Buffer const * GuiCompare::bufferFromFileName(string const & file) const Buffer const * GuiCompare::bufferFromFileName(string const & file) const
@ -256,10 +286,30 @@ int GuiCompare::run()
// new buffer that will carry the output // new buffer that will carry the output
FileName initpath(lyxrc.document_path); FileName initpath(lyxrc.document_path);
dest_buffer_ = newUnnamedFile(initpath, to_utf8(_("differences"))); dest_buffer_ = newUnnamedFile(initpath, to_utf8(_("differences")));
if (!new_buffer_ || !old_buffer_ || !dest_buffer_)
return 0;
dest_buffer_->changed(); dest_buffer_->changed();
dest_buffer_->markDirty(); dest_buffer_->markDirty();
return 0; // the comparison is done in a separate thread, so don't let
// the user change the buffers
old_buffer_->setReadonly(true);
new_buffer_->setReadonly(true);
// get the options from the dialog
CompareOptions options;
options.settings_from_new = newSettingsRB->isChecked();
// init the compare object and start it
compare_ = new Compare(new_buffer_, old_buffer_, dest_buffer_, options);
connect(compare_, SIGNAL(error()), this, SLOT(error()));
connect(compare_, SIGNAL(finished(bool)), this, SLOT(finished(bool)));
connect(compare_, SIGNAL(progress(int)), this, SLOT(progress(int)));
connect(compare_, SIGNAL(progressMax(int)), this, SLOT(progressMax(int)));
compare_->start(QThread::LowPriority);
return 1;
} }

View File

@ -16,6 +16,8 @@
#include "ui_CompareUi.h" #include "ui_CompareUi.h"
#include "qt_helpers.h" #include "qt_helpers.h"
#include "Compare.h"
namespace lyx { namespace lyx {
namespace frontend { namespace frontend {
@ -43,10 +45,12 @@ private Q_SLOTS:
/// ///
void selectOldFile(); void selectOldFile();
///
void error();
/// ///
void finished(bool aborted); void finished(bool aborted);
/// ///
void nextIt(int); void progress(int);
/// ///
void progressMax(int) const; void progressMax(int) const;
@ -79,6 +83,9 @@ private:
int run(); int run();
private: private:
/// the object that will do the comparison
Compare * compare_;
/// the buffer that will contain the result /// the buffer that will contain the result
Buffer * dest_buffer_; Buffer * dest_buffer_;
/// the buffer that will contain the result /// the buffer that will contain the result