2006-03-05 17:24:44 +00:00
|
|
|
/**
|
2007-09-03 05:59:32 +00:00
|
|
|
* \file ButtonController.cpp
|
2006-03-05 17:24:44 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Allan Rae
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-09-03 05:59:32 +00:00
|
|
|
#include "ButtonController.h"
|
2008-02-18 07:14:42 +00:00
|
|
|
|
2007-09-01 21:05:31 +00:00
|
|
|
#include "qt_helpers.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2008-02-18 07:14:42 +00:00
|
|
|
#include "support/debug.h"
|
2008-04-19 09:12:34 +00:00
|
|
|
#include "support/foreach.h"
|
2008-02-18 07:14:42 +00:00
|
|
|
|
2009-01-18 14:33:51 +00:00
|
|
|
#include <QCheckBox>
|
2006-03-05 17:24:44 +00:00
|
|
|
#include <QPushButton>
|
|
|
|
#include <QLineEdit>
|
2007-09-02 08:19:43 +00:00
|
|
|
#include <QLabel>
|
2008-04-19 09:12:34 +00:00
|
|
|
#include <QList>
|
2007-09-02 08:19:43 +00:00
|
|
|
#include <QValidator>
|
|
|
|
|
2008-04-19 09:12:34 +00:00
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
namespace lyx {
|
|
|
|
namespace frontend {
|
|
|
|
|
2008-04-19 09:12:34 +00:00
|
|
|
static void setWidgetEnabled(QWidget * obj, bool enabled)
|
|
|
|
{
|
|
|
|
if (QLineEdit * le = qobject_cast<QLineEdit*>(obj))
|
|
|
|
le->setReadOnly(!enabled);
|
|
|
|
else
|
|
|
|
obj->setEnabled(enabled);
|
|
|
|
|
|
|
|
obj->setFocusPolicy(enabled ? Qt::StrongFocus : Qt::NoFocus);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// CheckedLineEdit
|
|
|
|
//
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class CheckedLineEdit
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CheckedLineEdit(QLineEdit * input, QWidget * label = 0);
|
|
|
|
bool check() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// non-owned
|
|
|
|
QLineEdit * input_;
|
|
|
|
QWidget * label_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CheckedLineEdit::CheckedLineEdit(QLineEdit * input, QWidget * label)
|
|
|
|
: input_(input), label_(label)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
bool CheckedLineEdit::check() const
|
|
|
|
{
|
|
|
|
QValidator const * validator = input_->validator();
|
|
|
|
if (!validator)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
QString t = input_->text();
|
|
|
|
int p = 0;
|
|
|
|
bool const valid = validator->validate(t, p) == QValidator::Acceptable;
|
|
|
|
|
|
|
|
// Visual feedback.
|
|
|
|
setValid(input_, valid);
|
|
|
|
if (label_)
|
|
|
|
setValid(label_, valid);
|
|
|
|
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ButtonController::Private
|
|
|
|
//
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class ButtonController::Private
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef QList<CheckedLineEdit> CheckedWidgetList;
|
|
|
|
|
|
|
|
Private()
|
2009-01-18 14:33:51 +00:00
|
|
|
: okay_(0), apply_(0), cancel_(0), restore_(0), auto_apply_(0),
|
2008-04-19 09:12:34 +00:00
|
|
|
policy_(ButtonPolicy::IgnorantPolicy)
|
|
|
|
{}
|
|
|
|
|
|
|
|
/// \return true if all CheckedWidgets are in a valid state.
|
|
|
|
bool checkWidgets() const
|
|
|
|
{
|
|
|
|
bool valid = true;
|
|
|
|
foreach (const CheckedLineEdit & w, checked_widgets_)
|
|
|
|
valid &= w.check();
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
CheckedWidgetList checked_widgets_;
|
|
|
|
|
|
|
|
QPushButton * okay_;
|
|
|
|
QPushButton * apply_;
|
|
|
|
QPushButton * cancel_;
|
|
|
|
QPushButton * restore_;
|
2009-01-18 14:33:51 +00:00
|
|
|
QCheckBox * auto_apply_;
|
2008-04-19 09:12:34 +00:00
|
|
|
|
|
|
|
typedef QList<QWidget *> Widgets;
|
|
|
|
Widgets read_only_;
|
|
|
|
|
|
|
|
ButtonPolicy policy_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ButtonController
|
|
|
|
//
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2007-09-03 05:59:32 +00:00
|
|
|
ButtonController::ButtonController()
|
2008-04-19 09:12:34 +00:00
|
|
|
: d(new Private)
|
2006-03-05 17:24:44 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
2008-04-19 09:12:34 +00:00
|
|
|
ButtonController::~ButtonController()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-03 05:59:32 +00:00
|
|
|
void ButtonController::setPolicy(ButtonPolicy::Policy policy)
|
|
|
|
{
|
2008-04-19 09:12:34 +00:00
|
|
|
d->policy_.setPolicy(policy);
|
2007-09-03 05:59:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ButtonController::ok()
|
|
|
|
{
|
|
|
|
input(ButtonPolicy::SMI_OKAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ButtonController::input(ButtonPolicy::SMInput in)
|
|
|
|
{
|
|
|
|
if (ButtonPolicy::SMI_NOOP == in)
|
|
|
|
return;
|
2008-04-19 09:12:34 +00:00
|
|
|
d->policy_.input(in);
|
2007-09-03 05:59:32 +00:00
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ButtonController::apply()
|
|
|
|
{
|
|
|
|
input(ButtonPolicy::SMI_APPLY);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-18 14:33:51 +00:00
|
|
|
void ButtonController::autoApply()
|
|
|
|
{
|
|
|
|
input(ButtonPolicy::SMI_AUTOAPPLY);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-03 05:59:32 +00:00
|
|
|
void ButtonController::cancel()
|
|
|
|
{
|
|
|
|
input(ButtonPolicy::SMI_CANCEL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ButtonController::restore()
|
|
|
|
{
|
|
|
|
input(ButtonPolicy::SMI_RESTORE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ButtonController::hide()
|
|
|
|
{
|
|
|
|
input(ButtonPolicy::SMI_HIDE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ButtonController::setValid(bool v)
|
|
|
|
{
|
|
|
|
input(v ? ButtonPolicy::SMI_VALID : ButtonPolicy::SMI_INVALID);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ButtonController::setReadOnly(bool ro)
|
|
|
|
{
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::GUI, "Setting controller ro: " << ro);
|
2007-09-03 05:59:32 +00:00
|
|
|
|
2008-04-19 09:12:34 +00:00
|
|
|
d->policy_.input(ro ?
|
2007-09-03 05:59:32 +00:00
|
|
|
ButtonPolicy::SMI_READ_ONLY : ButtonPolicy::SMI_READ_WRITE);
|
2007-09-27 21:03:26 +00:00
|
|
|
// refreshReadOnly(); This will enable all widgets in dialogs, no matter if
|
|
|
|
// they allowed to be enabled, so when you plan to
|
|
|
|
// reenable this call, read this before:
|
|
|
|
// http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg128222.html
|
2007-09-03 05:59:32 +00:00
|
|
|
refresh();
|
|
|
|
return ro;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ButtonController::refresh() const
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::GUI, "Calling BC refresh()");
|
2007-09-01 20:44:14 +00:00
|
|
|
|
2008-04-19 09:12:34 +00:00
|
|
|
bool const all_valid = d->checkWidgets();
|
2007-09-01 20:44:14 +00:00
|
|
|
|
2008-04-19 09:12:34 +00:00
|
|
|
if (d->okay_) {
|
2007-09-01 20:44:14 +00:00
|
|
|
bool const enabled =
|
2007-09-03 05:59:32 +00:00
|
|
|
all_valid && policy().buttonStatus(ButtonPolicy::OKAY);
|
2008-04-19 09:12:34 +00:00
|
|
|
d->okay_->setEnabled(enabled);
|
2007-09-01 20:44:14 +00:00
|
|
|
}
|
2008-04-19 09:12:34 +00:00
|
|
|
if (d->apply_) {
|
2007-09-01 20:44:14 +00:00
|
|
|
bool const enabled =
|
2007-09-03 05:59:32 +00:00
|
|
|
all_valid && policy().buttonStatus(ButtonPolicy::APPLY);
|
2008-04-19 09:12:34 +00:00
|
|
|
d->apply_->setEnabled(enabled);
|
2007-09-01 20:44:14 +00:00
|
|
|
}
|
2008-04-19 09:12:34 +00:00
|
|
|
if (d->restore_) {
|
2007-09-01 20:44:14 +00:00
|
|
|
bool const enabled =
|
2007-09-03 05:59:32 +00:00
|
|
|
all_valid && policy().buttonStatus(ButtonPolicy::RESTORE);
|
2008-04-19 09:12:34 +00:00
|
|
|
d->restore_->setEnabled(enabled);
|
2007-09-01 20:44:14 +00:00
|
|
|
}
|
2008-04-19 09:12:34 +00:00
|
|
|
if (d->cancel_) {
|
2007-09-03 05:59:32 +00:00
|
|
|
bool const enabled = policy().buttonStatus(ButtonPolicy::CANCEL);
|
2007-09-01 20:44:14 +00:00
|
|
|
if (enabled)
|
2008-04-19 09:12:34 +00:00
|
|
|
d->cancel_->setText(qt_("Cancel"));
|
2007-09-01 20:44:14 +00:00
|
|
|
else
|
2008-04-19 09:12:34 +00:00
|
|
|
d->cancel_->setText(qt_("Close"));
|
2007-09-01 20:44:14 +00:00
|
|
|
}
|
2009-01-18 14:33:51 +00:00
|
|
|
if (d->auto_apply_) {
|
|
|
|
bool const enabled = policy().buttonStatus(ButtonPolicy::AUTOAPPLY);
|
|
|
|
d->auto_apply_->setEnabled(enabled);
|
|
|
|
}
|
|
|
|
|
2007-09-01 20:44:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-03 05:59:32 +00:00
|
|
|
void ButtonController::refreshReadOnly() const
|
2007-09-01 20:44:14 +00:00
|
|
|
{
|
2008-04-19 09:12:34 +00:00
|
|
|
if (d->read_only_.empty())
|
2007-09-02 07:53:07 +00:00
|
|
|
return;
|
2007-09-01 20:44:14 +00:00
|
|
|
|
2007-09-03 05:59:32 +00:00
|
|
|
bool const enable = !policy().isReadOnly();
|
2008-04-19 09:12:34 +00:00
|
|
|
|
|
|
|
foreach (QWidget * w, d->read_only_)
|
|
|
|
setWidgetEnabled(w, enable);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-19 09:12:34 +00:00
|
|
|
void ButtonController::addCheckedLineEdit(QLineEdit * input, QWidget * label)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2008-04-19 09:12:34 +00:00
|
|
|
d->checked_widgets_.append(CheckedLineEdit(input, label));
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
2007-09-02 07:53:07 +00:00
|
|
|
|
2008-04-19 09:12:34 +00:00
|
|
|
void ButtonController::setOK(QPushButton * obj)
|
2007-09-02 07:53:07 +00:00
|
|
|
{
|
2008-04-19 09:12:34 +00:00
|
|
|
d->okay_ = obj;
|
2007-09-02 07:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-19 09:12:34 +00:00
|
|
|
void ButtonController::setApply(QPushButton * obj)
|
2007-09-02 07:53:07 +00:00
|
|
|
{
|
2008-04-19 09:12:34 +00:00
|
|
|
d->apply_ = obj;
|
|
|
|
}
|
2007-09-02 07:53:07 +00:00
|
|
|
|
|
|
|
|
2009-01-18 14:33:51 +00:00
|
|
|
void ButtonController::setAutoApply(QCheckBox * obj)
|
|
|
|
{
|
|
|
|
d->auto_apply_ = obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-19 09:12:34 +00:00
|
|
|
void ButtonController::setCancel(QPushButton * obj)
|
|
|
|
{
|
|
|
|
d->cancel_ = obj;
|
2007-09-02 07:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-19 09:12:34 +00:00
|
|
|
void ButtonController::setRestore(QPushButton * obj)
|
|
|
|
{
|
|
|
|
d->restore_ = obj;
|
|
|
|
}
|
2007-09-02 08:19:43 +00:00
|
|
|
|
|
|
|
|
2008-04-19 09:12:34 +00:00
|
|
|
void ButtonController::addReadOnly(QWidget * obj)
|
2007-09-02 08:19:43 +00:00
|
|
|
{
|
2008-04-19 09:12:34 +00:00
|
|
|
d->read_only_.push_back(obj);
|
|
|
|
}
|
2007-09-02 08:19:43 +00:00
|
|
|
|
2008-04-19 09:12:34 +00:00
|
|
|
ButtonPolicy const & ButtonController::policy() const
|
|
|
|
{
|
|
|
|
return d->policy_;
|
|
|
|
}
|
2007-09-02 08:19:43 +00:00
|
|
|
|
|
|
|
|
2008-04-19 09:12:34 +00:00
|
|
|
ButtonPolicy & ButtonController::policy()
|
|
|
|
{
|
|
|
|
return d->policy_;
|
2007-09-02 08:19:43 +00:00
|
|
|
}
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|