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"
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
#include <QPushButton>
|
|
|
|
#include <QLineEdit>
|
2007-09-02 08:19:43 +00:00
|
|
|
#include <QLabel>
|
|
|
|
#include <QValidator>
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
namespace lyx {
|
|
|
|
namespace frontend {
|
|
|
|
|
2007-09-03 05:59:32 +00:00
|
|
|
ButtonController::ButtonController()
|
|
|
|
: okay_(0), apply_(0), cancel_(0), restore_(0),
|
|
|
|
policy_(ButtonPolicy::IgnorantPolicy)
|
2006-03-05 17:24:44 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
2007-09-03 05:59:32 +00:00
|
|
|
void ButtonController::setPolicy(ButtonPolicy::Policy policy)
|
|
|
|
{
|
|
|
|
policy_ = ButtonPolicy(policy);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ButtonController::ok()
|
|
|
|
{
|
|
|
|
input(ButtonPolicy::SMI_OKAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ButtonController::input(ButtonPolicy::SMInput in)
|
|
|
|
{
|
|
|
|
if (ButtonPolicy::SMI_NOOP == in)
|
|
|
|
return;
|
|
|
|
policy_.input(in);
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ButtonController::apply()
|
|
|
|
{
|
|
|
|
input(ButtonPolicy::SMI_APPLY);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
policy_.input(ro ?
|
|
|
|
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
|
|
|
|
|
|
|
bool const all_valid = checkWidgets();
|
|
|
|
|
|
|
|
if (okay_) {
|
|
|
|
bool const enabled =
|
2007-09-03 05:59:32 +00:00
|
|
|
all_valid && policy().buttonStatus(ButtonPolicy::OKAY);
|
2007-09-01 20:44:14 +00:00
|
|
|
okay_->setEnabled(enabled);
|
|
|
|
}
|
|
|
|
if (apply_) {
|
|
|
|
bool const enabled =
|
2007-09-03 05:59:32 +00:00
|
|
|
all_valid && policy().buttonStatus(ButtonPolicy::APPLY);
|
2007-09-01 20:44:14 +00:00
|
|
|
apply_->setEnabled(enabled);
|
|
|
|
}
|
|
|
|
if (restore_) {
|
|
|
|
bool const enabled =
|
2007-09-03 05:59:32 +00:00
|
|
|
all_valid && policy().buttonStatus(ButtonPolicy::RESTORE);
|
2007-09-01 20:44:14 +00:00
|
|
|
restore_->setEnabled(enabled);
|
|
|
|
}
|
|
|
|
if (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)
|
2007-11-15 20:04:51 +00:00
|
|
|
cancel_->setText(qt_("Cancel"));
|
2007-09-01 20:44:14 +00:00
|
|
|
else
|
2007-11-15 20:04:51 +00:00
|
|
|
cancel_->setText(qt_("Close"));
|
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
|
|
|
{
|
2007-09-02 07:53:07 +00:00
|
|
|
if (read_only_.empty())
|
|
|
|
return;
|
2007-09-01 20:44:14 +00:00
|
|
|
|
2007-09-03 05:59:32 +00:00
|
|
|
bool const enable = !policy().isReadOnly();
|
2007-09-01 20:44:14 +00:00
|
|
|
|
|
|
|
Widgets::const_iterator end = read_only_.end();
|
|
|
|
Widgets::const_iterator iter = read_only_.begin();
|
2007-09-02 07:53:07 +00:00
|
|
|
for (; iter != end; ++iter)
|
2007-09-01 20:44:14 +00:00
|
|
|
setWidgetEnabled(*iter, enable);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-03 05:59:32 +00:00
|
|
|
void ButtonController::setWidgetEnabled(QWidget * obj, bool enabled) const
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2007-04-25 16:39:21 +00:00
|
|
|
if (QLineEdit * le = qobject_cast<QLineEdit*>(obj))
|
2006-03-05 17:24:44 +00:00
|
|
|
le->setReadOnly(!enabled);
|
2007-04-25 16:39:21 +00:00
|
|
|
else
|
2006-03-05 17:24:44 +00:00
|
|
|
obj->setEnabled(enabled);
|
|
|
|
|
2007-04-25 16:39:21 +00:00
|
|
|
obj->setFocusPolicy(enabled ? Qt::StrongFocus : Qt::NoFocus);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
2007-09-02 07:53:07 +00:00
|
|
|
|
2007-09-03 05:59:32 +00:00
|
|
|
void ButtonController::addCheckedLineEdit(QLineEdit * input, QWidget * label)
|
2007-09-02 07:53:07 +00:00
|
|
|
{
|
2007-09-02 08:19:43 +00:00
|
|
|
checked_widgets.push_back(CheckedLineEdit(input, label));
|
2007-09-02 07:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-03 05:59:32 +00:00
|
|
|
bool ButtonController::checkWidgets() const
|
2007-09-02 07:53:07 +00:00
|
|
|
{
|
|
|
|
bool valid = true;
|
|
|
|
|
|
|
|
CheckedWidgetList::const_iterator it = checked_widgets.begin();
|
|
|
|
CheckedWidgetList::const_iterator end = checked_widgets.end();
|
|
|
|
|
|
|
|
for (; it != end; ++it)
|
2007-09-02 08:19:43 +00:00
|
|
|
valid &= it->check();
|
2007-09-02 07:53:07 +00:00
|
|
|
|
|
|
|
// return valid status after checking ALL widgets
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-02 08:19:43 +00:00
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// CheckedLineEdit
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
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.
|
2007-09-15 13:06:48 +00:00
|
|
|
setValid(input_, valid);
|
|
|
|
if (label_)
|
|
|
|
setValid(label_, valid);
|
2007-09-02 08:19:43 +00:00
|
|
|
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|