/** * \file qt2/Toolbar_pimpl.C * This file is part of LyX, the document processor. * Licence details can be found in the file COPYING. * * \author Lars Gullik Bjønnes * \author John Levon * * Full author contact details are available in file CREDITS */ #include #include "ToolbarBackend.h" #include "debug.h" #include "gettext.h" #include "lyxfunc.h" #include "FuncStatus.h" #include "BufferView.h" #include "buffer.h" #include "LyXAction.h" #include "qt_helpers.h" #include "support/LAssert.h" #include "support/filetools.h" #include "support/lstrings.h" #include #include "QtView.h" #include "Toolbar_pimpl.h" #include #include #include #include using std::endl; class QLComboBox : public QComboBox { public: QLComboBox(QWidget * parent) : QComboBox(parent) {} void popup() { QComboBox::popup(); } }; Toolbar::Pimpl::Pimpl(LyXView * o, int, int) : owner_(static_cast(o)), combo_(0) { proxy_.reset(new ToolbarProxy(*this)); } Toolbar::Pimpl::~Pimpl() { } void Toolbar::Pimpl::update() { ButtonMap::const_iterator p = map_.begin(); ButtonMap::const_iterator end = map_.end(); for (; p != end; ++p) { QToolButton * button = p->first; int action = p->second; FuncStatus const status = owner_->getLyXFunc().getStatus(action); button->setToggleButton(true); button->setOn(status.onoff(true)); button->setEnabled(!status.disabled()); } if (combo_) combo_->setEnabled(!owner_->getLyXFunc().getStatus(LFUN_LAYOUT).disabled()); } void Toolbar::Pimpl::button_selected(QToolButton * button) { ButtonMap::const_iterator cit = map_.find(button); if (cit == map_.end()) { lyxerr << "non existent tool button selected !" << endl; return; } owner_->getLyXFunc().dispatch(cit->second, true); } void Toolbar::Pimpl::changed_layout(string const & sel) { owner_->centralWidget()->setFocus(); LyXTextClass const & tc = owner_->buffer()->params.getLyXTextClass(); LyXTextClass::const_iterator end = tc.end(); for (LyXTextClass::const_iterator cit = tc.begin(); cit != end; ++cit) { // Yes, the _() is correct if (_((*cit)->name()) == sel) { owner_->getLyXFunc().dispatch(FuncRequest(LFUN_LAYOUT, (*cit)->name()), true); return; } } lyxerr << "ERROR (Toolbar::Pimpl::layoutSelected): layout not found!" << endl; } void Toolbar::Pimpl::setLayout(string const & layout) { if (!combo_) return; LyXTextClass const & tc = owner_->buffer()->params.getLyXTextClass(); QString const & name = qt_(tc[layout]->name()); int i = 0; for (; i < combo_->count(); ++i) { if (name == combo_->text(i)) break; } if (i == combo_->count()) { lyxerr << "Trying to select non existent layout type " << fromqstr(name) << endl; return; } combo_->setCurrentItem(i); } void Toolbar::Pimpl::updateLayoutList(bool force) { if (!combo_) return; // if we don't need an update, don't ... if (combo_->count() && !force) return; LyXTextClass const & tc = owner_->buffer()->params.getLyXTextClass(); combo_->setUpdatesEnabled(false); combo_->clear(); LyXTextClass::const_iterator cit = tc.begin(); LyXTextClass::const_iterator end = tc.end(); for (; cit != end; ++cit) { // ignore obsolete entries if ((*cit)->obsoleted_by().empty()) combo_->insertItem(qt_((*cit)->name())); } // needed to recalculate size hint combo_->hide(); combo_->setMinimumWidth(combo_->sizeHint().width()); combo_->show(); combo_->setUpdatesEnabled(true); combo_->update(); } void Toolbar::Pimpl::clearLayoutList() { if (!combo_) return; combo_->clear(); } void Toolbar::Pimpl::openLayoutList() { if (!combo_) return; combo_->popup(); } void Toolbar::Pimpl::add(int action, string const & tooltip) { if (!toolbars_.size()) { toolbars_.push_back(new QToolBar(owner_)); } switch (action) { case ToolbarBackend::SEPARATOR: toolbars_.back()->addSeparator(); break; case ToolbarBackend::NEWLINE: toolbars_.push_back(new QToolBar(owner_)); break; case ToolbarBackend::LAYOUTS: { combo_ = new QLComboBox(toolbars_.back()); QSizePolicy p(QSizePolicy::Minimum, QSizePolicy::Fixed); combo_->setSizePolicy(p); combo_->setFocusPolicy(QWidget::ClickFocus); combo_->setMinimumWidth(combo_->sizeHint().width()); QObject::connect(combo_, SIGNAL(activated(const QString &)), proxy_.get(), SLOT(layout_selected(const QString &))); break; } default: { QPixmap p = QPixmap(toolbarbackend.getIcon(action).c_str()); QToolButton * tb = new QToolButton(p, toqstr(tooltip), "", proxy_.get(), SLOT(button_selected()), toolbars_.back()); map_[tb] = action; break; } } }