Inset::validateModifyArgument(): new virtual interface for using LFUN_INSET_MODIFY.

GuiInfo: extract generic code into new class InsetDialog.


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@33270 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Abdelrazak Younes 2010-01-30 11:15:05 +00:00
parent 10caadcdbe
commit d3487d106c
9 changed files with 173 additions and 65 deletions

View File

@ -782,6 +782,7 @@ src_frontends_qt4_header_files = Split('''
GuiWrap.h
IconPalette.h
InsertTableWidget.h
InsetDialog.h
LaTeXHighlighter.h
LayoutBox.h
LengthCombo.h
@ -882,6 +883,7 @@ src_frontends_qt4_files = Split('''
GuiWrap.cpp
IconPalette.cpp
InsertTableWidget.cpp
InsetDialog.cpp
LengthCombo.cpp
LaTeXHighlighter.cpp
LayoutBox.cpp

View File

@ -48,7 +48,7 @@ char const * info_types_gui[] =
GuiInfo::GuiInfo(GuiView & lv)
: DialogView(lv, "info", qt_("Info"))
: InsetDialog(lv, INFO_CODE, "info", qt_("Info"))
{
setupUi(this);
@ -61,18 +61,13 @@ GuiInfo::GuiInfo(GuiView & lv)
void GuiInfo::on_newPB_clicked()
{
dialogToParams();
docstring const argument = qstring_to_ucs4(type_ + ' ' + name_);
// FIXME: if we used a standard LFUN_INSET_INSERT command,
// This slot could be transferred to InsetDialog.
docstring const argument = dialogToParams();
dispatch(FuncRequest(LFUN_INFO_INSERT, argument));
}
void GuiInfo::on_closePB_clicked()
{
hide();
}
void GuiInfo::on_typeCO_currentIndexChanged(int)
{
applyView();
@ -85,58 +80,32 @@ void GuiInfo::on_nameLE_textChanged(QString const &)
}
void GuiInfo::applyView()
{
InsetInfo const * ii = dynamic_cast<InsetInfo const *>(inset(INFO_CODE));
if (!ii)
return;
dialogToParams();
docstring const argument = qstring_to_ucs4(type_ + ' ' + name_);
if (!ii->validate(argument))
return;
dispatch(FuncRequest(LFUN_INSET_MODIFY, argument));
// FIXME: update the inset contents
bufferview()->buffer().updateLabels();
}
void GuiInfo::updateView()
{
InsetInfo const * ii = dynamic_cast<InsetInfo const *>(inset(INFO_CODE));
if (!ii) {
enableView(false);
return;
}
type_ = toqstr(ii->infoType());
name_ = toqstr(ii->infoName());
paramsToDialog();
}
void GuiInfo::paramsToDialog()
void GuiInfo::paramsToDialog(Inset const * inset)
{
InsetInfo const * ii = static_cast<InsetInfo const *>(inset);
QString const type = toqstr(ii->infoType());
QString const name = toqstr(ii->infoName());
typeCO->blockSignals(true);
nameLE->blockSignals(true);
int type = findToken(info_types, fromqstr(type_));
typeCO->setCurrentIndex(type >= 0 ? type : 0);
// Without this test, 'math-insert' (name_) will replace 'math-insert '
int type_index = findToken(info_types, fromqstr(type));
typeCO->setCurrentIndex(type_index >= 0 ? type_index : 0);
// Without this test, 'math-insert' (name) will replace 'math-insert '
// in nameLE and effectively disallow the input of spaces after a LFUN.
if (nameLE->text().trimmed() != name_)
nameLE->setText(name_);
if (nameLE->text().trimmed() != name)
nameLE->setText(name);
typeCO->blockSignals(false);
nameLE->blockSignals(false);
}
void GuiInfo::dialogToParams()
docstring GuiInfo::dialogToParams() const
{
int type = typeCO->currentIndex();
if (type != -1)
type_ = info_types[type];
name_ = nameLE->text();
int type_index = typeCO->currentIndex();
QString type;
if (type_index != -1)
type = info_types[type_index];
QString const name = nameLE->text();
return qstring_to_ucs4(type + ' ' + name);
}

View File

@ -12,14 +12,16 @@
#ifndef GUI_INFO_H
#define GUI_INFO_H
#include "DialogView.h"
#include "InsetDialog.h"
#include "ui_InfoUi.h"
namespace lyx {
class Inset;
namespace frontend {
class GuiInfo : public DialogView, public Ui::InfoUi
class GuiInfo : public InsetDialog, public Ui::InfoUi
{
Q_OBJECT
@ -28,25 +30,20 @@ public:
/// \name Dialog inherited methods
//@{
void applyView();
void updateView();
void dispatchParams() {}
void enableView(bool enable);
bool isBufferDependent() const { return true; }
bool canApply() const { return true; }
//@}
private Q_SLOTS:
void on_newPB_clicked();
void on_closePB_clicked();
void on_typeCO_currentIndexChanged(int);
void on_nameLE_textChanged(QString const &);
private:
void paramsToDialog();
void dialogToParams();
QString type_;
QString name_;
/// \name InsetDialog inherited methods
//@{
void paramsToDialog(Inset const *);
docstring dialogToParams() const;
//@}
};
} // namespace frontend

View File

@ -0,0 +1,80 @@
/**
* \file InsetDialog.cpp
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Abdelrazak Younes
*
* Full author contact details are available in file CREDITS.
*/
#include <config.h>
#include "InsetDialog.h"
#include "qt_helpers.h"
#include "Buffer.h"
#include "buffer_funcs.h"
#include "BufferParams.h"
#include "BufferView.h"
#include "Cursor.h"
#include "FuncRequest.h"
#include "support/debug.h"
#include "support/lstrings.h"
using namespace std;
using namespace lyx::support;
namespace lyx {
namespace frontend {
/////////////////////////////////////////////////////////////////
//
// InsetDialog
//
/////////////////////////////////////////////////////////////////
InsetDialog::InsetDialog(GuiView & lv, InsetCode code,
QString const & name, QString const & title)
: DialogView(lv, name, title), code_(code)
{
}
void InsetDialog::on_closePB_clicked()
{
hide();
}
void InsetDialog::applyView()
{
Inset const * i = inset(code_);
if (!i)
return;
docstring const argument = dialogToParams();
if (!i->validateModifyArgument(argument))
return;
dispatch(FuncRequest(LFUN_INSET_MODIFY, argument));
}
void InsetDialog::updateView()
{
Inset const * i = inset(code_);
if (i)
paramsToDialog(i);
else
enableView(false);
}
} // namespace frontend
} // namespace lyx
#include "moc_InsetDialog.cpp"

View File

@ -0,0 +1,55 @@
// -*- C++ -*-
/**
* \file InsetDialog.h
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Abdelrazak Younes
*
* Full author contact details are available in file CREDITS.
*/
#ifndef INSET_DIALOG_H
#define INSET_DIALOG_H
#include "DialogView.h"
namespace lyx {
class Inset;
namespace frontend {
class InsetDialog : public DialogView
{
Q_OBJECT
public:
InsetDialog(GuiView & lv, InsetCode code,
QString const & name, QString const & title);
/// \name DialogView inherited methods
//@{
void applyView();
void updateView();
void dispatchParams() {}
bool isBufferDependent() const { return true; }
bool canApply() const { return true; }
//@}
protected Q_SLOTS:
void on_closePB_clicked();
protected:
///
virtual void paramsToDialog(Inset const *) = 0;
///
virtual docstring dialogToParams() const = 0;
///
InsetCode const code_;
};
} // namespace frontend
} // namespace lyx
#endif // INSET_DIALOG_H

View File

@ -132,6 +132,7 @@ SOURCEFILES = \
GuiWrap.cpp \
IconPalette.cpp \
InsertTableWidget.cpp \
InsetDialog.cpp \
LengthCombo.cpp \
LyXFileDialog.cpp \
LaTeXHighlighter.cpp \
@ -234,6 +235,7 @@ MOCHEADER = \
GuiWrap.h \
IconPalette.h \
InsertTableWidget.h \
InsetDialog.h \
LayoutBox.h \
LengthCombo.h \
LyXFileDialog.h \

View File

@ -291,6 +291,9 @@ public:
/// request "external features"
virtual void validate(LaTeXFeatures &) const {}
/// Validate LFUN_INSET_MODIFY argument.
virtual bool validateModifyArgument(docstring const &) const { return true; }
/// describe content if cursor inside
virtual void infoize(odocstream &) const {}
/// describe content if cursor behind

View File

@ -150,7 +150,7 @@ void InsetInfo::write(ostream & os) const
}
bool InsetInfo::validate(docstring const & arg) const
bool InsetInfo::validateModifyArgument(docstring const & arg) const
{
string type;
string const name = trim(split(to_utf8(arg), type, ' '));

View File

@ -110,7 +110,7 @@ public:
///
std::string infoName() const { return name_; }
///
bool validate(docstring const & argument) const;
bool validateModifyArgument(docstring const & argument) const;
///
bool showInsetDialog(BufferView * bv) const;
///