]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiParagraph.cpp
* fix spelling in comments to please John.
[lyx.git] / src / frontends / qt4 / GuiParagraph.cpp
index 8b40befb6b748e00e3d2fc3cc3d364fe1547c5a4..6cad13767308076f0881beec28e7ee244d43d5a6 100644 (file)
@@ -5,6 +5,8 @@
  *
  * \author Edwin Leuven
  * \author Richard Heck
+ * \author Abdelrazak Younes
+ * \author Angus Leeming
  *
  * Full author contact details are available in file CREDITS.
  */
 
 #include "GuiParagraph.h"
 
-#include "debug.h"
-#include "frontend_helpers.h"
-#include "ParagraphParameters.h"
 #include "qt_helpers.h"
+
+#include "Buffer.h"
+#include "BufferParams.h"
+#include "BufferView.h"
+#include "Cursor.h"
+#include "FuncRequest.h"
+#include "GuiView.h"
+#include "Lexer.h"
+#include "Paragraph.h"
+#include "ParagraphParameters.h"
 #include "Spacing.h"
 
+#include "support/debug.h"
+#include "support/gettext.h"
+
 #include <QCheckBox>
-#include <QCloseEvent>
 #include <QLineEdit>
 #include <QPushButton>
+#include <QSettings>
+#include <QVariant>
 
-using std::string;
-using std::endl;
-
-/////////////////////////////////////////////////////////////////////
-//
-// GuiParagraphDialog
-//
-/////////////////////////////////////////////////////////////////////
-
+#include <sstream>
 
-#include <boost/current_function.hpp>
+using namespace std;
 
 namespace lyx {
 namespace frontend {
 
-GuiParagraphDialog::GuiParagraphDialog(GuiParagraph * form)
-       : form_(form)
+GuiParagraph::GuiParagraph(GuiView & lv)
+       : DialogView(lv, "paragraph", qt_("Paragraph Settings"))
 {
        setupUi(this);
 
-       connect(okPB, SIGNAL(clicked()), form_, SLOT(slotOK()));
-       connect(applyPB, SIGNAL(clicked()), form_, SLOT(slotApply()));
-       connect(closePB, SIGNAL(clicked()), form_, SLOT(slotClose()));
-       connect(restorePB, SIGNAL(clicked()), form_, SLOT(slotRestore()));
-       connect(alignDefaultRB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
-       connect(alignJustRB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
-       connect(alignLeftRB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
-       connect(alignRightRB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
-       connect(alignCenterRB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
-       connect(linespacing, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
-       connect(linespacing, SIGNAL(activated(int)),
-               this, SLOT(enableLinespacingValue(int)));
-       connect(linespacingValue, SIGNAL(textChanged(const QString &)),
-               this, SLOT(change_adaptor()));
-       connect(indentCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
-       connect(labelWidth, SIGNAL(textChanged(const QString &)),
-               this, SLOT(change_adaptor()));
-
+       connect(alignDefaultRB, SIGNAL(clicked()), this, SLOT(changed()));
+       connect(alignJustRB, SIGNAL(clicked()), this, SLOT(changed()));
+       connect(alignLeftRB, SIGNAL(clicked()), this, SLOT(changed()));
+       connect(alignRightRB, SIGNAL(clicked()), this, SLOT(changed()));
+       connect(alignCenterRB, SIGNAL(clicked()), this, SLOT(changed()));
+       connect(linespacing, SIGNAL(activated(int)), this, SLOT(changed()));
+       connect(linespacingValue, SIGNAL(textChanged(QString)),
+               this, SLOT(changed()));
+       connect(indentCB, SIGNAL(clicked()), this, SLOT(changed()));
+       connect(labelWidth, SIGNAL(textChanged(QString)),
+               this, SLOT(changed()));
+
+#ifdef Q_WS_MACX
+       // On Mac it's common to have tool windows which are always in the
+       // foreground and are hidden when the main window is not focused.
+       setWindowFlags(Qt::Tool);
+       synchronizedViewCB->setChecked(true);
+       closePB->setText(qt_("&Cancel"));
+#else
+       synchronizedViewCB->setChecked(false);
+#endif
+
+       on_synchronizedViewCB_toggled();
        linespacingValue->setValidator(new QDoubleValidator(linespacingValue));
 
-       labelWidth->setWhatsThis( qt_(
+       labelWidth->setWhatsThis(qt_(
                "As described in the User Guide, the width of"
                " this text determines the width of the label part"
                " of each item in environments like List and"
@@ -75,77 +85,61 @@ GuiParagraphDialog::GuiParagraphDialog(GuiParagraph * form)
                " items is used."
        ));
 
-       radioMap[LYX_ALIGN_LAYOUT] = alignDefaultRB;
-       radioMap[LYX_ALIGN_BLOCK]  = alignJustRB;
-       radioMap[LYX_ALIGN_LEFT]   = alignLeftRB;
-       radioMap[LYX_ALIGN_RIGHT]  = alignRightRB;
-       radioMap[LYX_ALIGN_CENTER] = alignCenterRB;
-       
-       labelMap[LYX_ALIGN_LAYOUT] = _("Use Paragraph's Default Alignment");
-       labelMap[LYX_ALIGN_BLOCK]  = _("Justified");
-       labelMap[LYX_ALIGN_LEFT]   = _("Left");
-       labelMap[LYX_ALIGN_RIGHT]  = _("Right");
-       labelMap[LYX_ALIGN_CENTER] = _("Center");
-}
+       radioMap_[LYX_ALIGN_LAYOUT] = alignDefaultRB;
+       radioMap_[LYX_ALIGN_BLOCK]  = alignJustRB;
+       radioMap_[LYX_ALIGN_LEFT]   = alignLeftRB;
+       radioMap_[LYX_ALIGN_RIGHT]  = alignRightRB;
+       radioMap_[LYX_ALIGN_CENTER] = alignCenterRB;
 
-
-void GuiParagraphDialog::closeEvent(QCloseEvent * e)
-{
-       form_->slotWMHide();
-       e->accept();
+       alignDefaultLabel_ = alignDefaultRB->text();
 }
 
 
-void GuiParagraphDialog::change_adaptor()
+void GuiParagraph::on_linespacing_activated(int index)
 {
-       form_->changed();
+       linespacingValue->setEnabled(index == 4);
 }
 
 
-void GuiParagraphDialog::enableLinespacingValue(int)
+void GuiParagraph::checkAlignmentRadioButtons()
 {
-       bool const enable = linespacing->currentIndex() == 4;
-       linespacingValue->setEnabled(enable);
-}
-
-
-void GuiParagraphDialog::checkAlignmentRadioButtons() {
-       LyXAlignment const alignPossible = form_->controller().alignPossible();
+       static std::map<LyXAlignment, QString> labelMap_;
+       if (labelMap_.empty()) {
+               labelMap_[LYX_ALIGN_BLOCK]  = qt_("Justified");
+               labelMap_[LYX_ALIGN_LEFT]   = qt_("Left");
+               labelMap_[LYX_ALIGN_RIGHT]  = qt_("Right");
+               labelMap_[LYX_ALIGN_CENTER] = qt_("Center");
+       }
 
-       QPRadioMap::iterator it = radioMap.begin();
-       for (; it != radioMap.end(); ++it) {
+       RadioMap::iterator it = radioMap_.begin();
+       for (; it != radioMap_.end(); ++it) {
                LyXAlignment const align = it->first;
-               it->second->setEnabled(align & alignPossible);
+               it->second->setEnabled(align & alignPossible());
        }
-       docstring label = labelMap[LYX_ALIGN_LAYOUT];
-       if (!form_->controller().haveMulitParSelection())
-               label += (" (" + labelMap[form_->controller().alignDefault()] + ")");
-       alignDefaultRB->setText(toqstr(label));
+       if (haveMultiParSelection())
+               alignDefaultRB->setText(alignDefaultLabel_);
+       else
+               alignDefaultRB->setText(alignDefaultLabel_ + " ("
+                       + labelMap_[alignDefault()] + ")");
 }
 
 
-void GuiParagraphDialog::alignmentToRadioButtons(LyXAlignment align)
+void GuiParagraph::alignmentToRadioButtons(LyXAlignment align)
 {
-       QPRadioMap::const_iterator it = radioMap.begin();
-       for (;it != radioMap.end(); ++it) {
-               if (align == it->first) {
-                       it->second->blockSignals(true);
-                       it->second->setChecked(true);
-                       it->second->blockSignals(false);
-                       return;
-               }
+       RadioMap::const_iterator it = radioMap_.begin();
+       for (;it != radioMap_.end(); ++it) {
+               it->second->blockSignals(true);
+               it->second->setChecked(align == it->first);
+               it->second->blockSignals(false);
        }
-
-       lyxerr << BOOST_CURRENT_FUNCTION << "Unknown alignment "
-               << align << std::endl;
 }
 
 
-LyXAlignment GuiParagraphDialog::getAlignmentFromDialog()
+LyXAlignment GuiParagraph::getAlignmentFromDialog() const
 {
        LyXAlignment alignment = LYX_ALIGN_NONE;
-       QPRadioMap::const_iterator it = radioMap.begin();
-       for (; it != radioMap.end(); ++it) {
+       RadioMap::const_iterator it = radioMap_.begin();
+       for (; it != radioMap_.end(); ++it) {
                if (it->second->isChecked()) {
                        alignment = it->first;
                        break;
@@ -155,124 +149,242 @@ LyXAlignment GuiParagraphDialog::getAlignmentFromDialog()
 }
 
 
-/////////////////////////////////////////////////////////////////////
-//
-// GuiParagraph
-//
-/////////////////////////////////////////////////////////////////////
+void GuiParagraph::on_synchronizedViewCB_toggled()
+{
+       bool in_sync = synchronizedViewCB->isChecked();
+       restorePB->setEnabled(!in_sync);
+       applyPB->setEnabled(!in_sync);
+       okPB->setEnabled(!in_sync);
+       if (!in_sync)
+               closePB->setText(qt_("&Cancel"));
+       else
+               closePB->setText(qt_("&Close"));
+}
 
 
-GuiParagraph::GuiParagraph(GuiDialog & parent)
-       : GuiView<GuiParagraphDialog>(parent, _("Paragraph Settings"))
-{}
+void GuiParagraph::changed()
+{
+       if (synchronizedViewCB->isChecked())
+               on_applyPB_clicked();
+}
 
 
-void GuiParagraph::build_dialog()
+void GuiParagraph::on_applyPB_clicked()
 {
-       // the dialog
-       dialog_.reset(new GuiParagraphDialog(this));
-
-       // Manage the ok, apply, restore and cancel/close buttons
-       bc().setOK(dialog_->okPB);
-       bc().setApply(dialog_->applyPB);
-       bc().setCancel(dialog_->closePB);
-       bc().setRestore(dialog_->restorePB);
+       applyView();
 }
 
 
-void GuiParagraph::apply()
+void GuiParagraph::on_okPB_clicked()
 {
-       ParagraphParameters & params = controller().params();
+       applyView();
+       hide();
+}
+
+
+void GuiParagraph::on_closePB_clicked()
+{
+       hide();
+}
+
+
+void GuiParagraph::on_restorePB_clicked()
+{
+       updateView();
+}
+
 
-       params.align(dialog_->getAlignmentFromDialog());
+void GuiParagraph::applyView()
+{
+       params_ = params();
+
+       params_.align(getAlignmentFromDialog());
 
        // get spacing
-       Spacing::Space linespacing = Spacing::Default;
+       Spacing::Space ls = Spacing::Default;
        string other;
-       switch (dialog_->linespacing->currentIndex()) {
+       switch (linespacing->currentIndex()) {
        case 0:
-               linespacing = Spacing::Default;
+               ls = Spacing::Default;
                break;
        case 1:
-               linespacing = Spacing::Single;
+               ls = Spacing::Single;
                break;
        case 2:
-               linespacing = Spacing::Onehalf;
+               ls = Spacing::Onehalf;
                break;
        case 3:
-               linespacing = Spacing::Double;
+               ls = Spacing::Double;
                break;
        case 4:
-               linespacing = Spacing::Other;
-               other = fromqstr(dialog_->linespacingValue->text());
+               ls = Spacing::Other;
+               other = widgetToDoubleStr(linespacingValue);
                break;
        }
 
-       Spacing const spacing(linespacing, other);
-       params.spacing(spacing);
+       Spacing const spacing(ls, other);
+       params_.spacing(spacing);
 
        // label width
-       params.labelWidthString(qstring_to_ucs4(dialog_->labelWidth->text()));
+       params_.labelWidthString(qstring_to_ucs4(labelWidth->text()));
        // indendation
-       params.noindent(!dialog_->indentCB->isChecked());
+       params_.noindent(!indentCB->isChecked());
+
+       dispatchParams();
 }
 
 
-void GuiParagraph::update_contents()
+void GuiParagraph::updateView()
 {
-       ParagraphParameters const & params = controller().params();
+       on_synchronizedViewCB_toggled();
+
+       ParagraphParameters const & pp = params();
 
        // label width
-       docstring const & labelwidth = params.labelWidthString();
-       // FIXME We should not compare translated strings
-       if (labelwidth != _("Senseless with this layout!")) {
-               dialog_->labelwidthGB->setEnabled(true);
-               dialog_->labelWidth->setText(toqstr(labelwidth));
+       docstring const & labelwidth = pp.labelWidthString();
+       if (hasLabelwidth()) {
+               labelwidthGB->setEnabled(true);
+               labelWidth->setText(toqstr(labelwidth));
        } else {
-               dialog_->labelwidthGB->setEnabled(false);
-               dialog_->labelWidth->setText("");
+               labelwidthGB->setEnabled(false);
+               labelWidth->setText(QString());
        }
 
        // alignment
-       dialog_->checkAlignmentRadioButtons();
-       dialog_->alignmentToRadioButtons(params.align());
+       checkAlignmentRadioButtons();
+       alignmentToRadioButtons(pp.align());
 
        //indentation
-       bool const canindent = controller().canIndent();
-       dialog_->indentCB->setEnabled(canindent);
-       dialog_->indentCB->setChecked(canindent && !params.noindent());
+       bool const canindent = canIndent();
+       indentCB->setEnabled(canindent);
+       indentCB->setChecked(canindent && !pp.noindent());
 
        // linespacing
-       int linespacing;
-       Spacing const & space = params.spacing();
+       int ls;
+       Spacing const & space = pp.spacing();
        switch (space.getSpace()) {
        case Spacing::Single:
-               linespacing = 1;
+               ls = 1;
                break;
        case Spacing::Onehalf:
-               linespacing = 2;
+               ls = 2;
                break;
        case Spacing::Double:
-               linespacing = 3;
+               ls = 3;
                break;
        case Spacing::Other:
-               linespacing = 4;
+               ls = 4;
                break;
        default:
-               linespacing = 0;
+               ls = 0;
                break;
        }
-       dialog_->linespacing->setCurrentIndex(linespacing);
+       linespacing->setCurrentIndex(ls);
        if (space.getSpace() == Spacing::Other) {
-               dialog_->linespacingValue->setText(toqstr(space.getValueAsString()));
-               dialog_->linespacingValue->setEnabled(true);
+               doubleToWidget(linespacingValue, space.getValue());
+               linespacingValue->setEnabled(true);
        } else {
-               dialog_->linespacingValue->setText("");
-               dialog_->linespacingValue->setEnabled(false);
+               linespacingValue->setText(QString());
+               linespacingValue->setEnabled(false);
        }
 }
 
+
+void GuiParagraph::enableView(bool enable)
+{
+       indentCB->setEnabled(enable);
+       linespacing->setEnabled(enable);
+       labelWidth->setEnabled(enable);
+       synchronizedViewCB->setEnabled(enable);
+       applyPB->setEnabled(enable);
+       restorePB->setEnabled(enable);
+       if (!enable)
+               synchronizedViewCB->setChecked(true);
+       RadioMap::const_iterator it = radioMap_.begin();
+       for (; it != radioMap_.end(); ++it)
+               it->second->setEnabled(enable);
+}
+
+
+ParagraphParameters const & GuiParagraph::params() const
+{
+       if (haveMultiParSelection()) {
+               // FIXME: in case of multi-paragraph selection, it would be nice to
+               // initialise the parameters that are common to all paragraphs.
+               static ParagraphParameters empty;
+               return empty;
+       }
+       return bufferview()->cursor().innerParagraph().params();
+}
+
+
+void GuiParagraph::dispatchParams()
+{
+       ostringstream data;
+       params_.write(data);
+       FuncRequest const fr(getLfun(), data.str());
+       dispatch(fr);
+}
+
+
+bool GuiParagraph::haveMultiParSelection() const
+{
+       Cursor const & cur = bufferview()->cursor();
+       return cur.selection() && cur.selBegin().pit() != cur.selEnd().pit();
+}
+
+       
+bool GuiParagraph::canIndent() const
+{
+       return buffer().params().paragraph_separation
+               == BufferParams::ParagraphIndentSeparation;
+}
+
+
+LyXAlignment GuiParagraph::alignPossible() const
+{
+       return bufferview()->cursor().innerParagraph().layout().alignpossible;
+}
+
+
+LyXAlignment GuiParagraph::alignDefault() const
+{
+       return bufferview()->cursor().innerParagraph().layout().align;
+}
+
+
+bool GuiParagraph::hasLabelwidth() const
+{
+       Layout layout = bufferview()->cursor().innerParagraph().layout();
+       return (layout.margintype == MARGIN_MANUAL
+               || layout.latextype == LATEX_BIB_ENVIRONMENT);
+}
+
+
+void GuiParagraph::saveSession() const
+{
+       Dialog::saveSession();
+       QSettings settings;
+       settings.setValue(sessionKey() + "/autoapply", synchronizedViewCB->isChecked());
+}
+
+
+void GuiParagraph::restoreSession()
+{
+       Dialog::restoreSession();
+       QSettings settings;
+       synchronizedViewCB->setChecked(
+               settings.value(sessionKey() + "/autoapply").toBool());
+}
+
+
+Dialog * createGuiParagraph(GuiView & lv)
+{
+       return new GuiParagraph(lv);
+}
+
+
 } // namespace frontend
 } // namespace lyx
 
-#include "GuiParagraph_moc.cpp"
+#include "moc_GuiParagraph.cpp"