/*! @file @id $Id$ */ // 1 2 3 4 5 6 7 8 // 45678901234567890123456789012345678901234567890123456789012345678901234567890 #ifndef __BUTTONLINEEDIT_HXX__ #define __BUTTONLINEEDIT_HXX__ #include #include #include #include #include #include //! @addtogroup qbrowserlib //! @{ //! A QLineEdit that may have buttons to the right within the lineedit. class ButtonLineEdit: public QLineEdit { Q_OBJECT; public: ButtonLineEdit(QWidget* p=0): QLineEdit(p) { TRC; } QToolButton* add(QAction* a) { TRC; QToolButton* b(new QToolButton(this)); b->setDefaultAction(a); add(b); return b; } ButtonLineEdit& add(QToolButton* b) { TRC; b->setParent(this); b->setStyleSheet("QToolButton { border: none; padding: 0; }"); b->setCursor(Qt::ArrowCursor); _buttons.push_back(b); resizeEvent(0); return *this; } ButtonLineEdit& changeStyleSheet(QString s) { TRC; _style = s; resizeEvent(0); return *this; } static void foreachActionWidget(QAction& a, void(QWidget::*f)()) { QList widgets(a.associatedWidgets()); for (QList::iterator w(widgets.begin()); w!=widgets.end(); ++w) ((*w)->*f)(); } protected: void resizeEvent(QResizeEvent*) { QSize sz; int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); for (Buttons::iterator it(_buttons.begin()); it!=_buttons.end(); ++it) { if (sz.isEmpty()) sz = (*it)->sizeHint(); else sz.setWidth(sz.width()+(*it)->sizeHint().width()); (*it)->move(rect().right() - frameWidth - sz.width(), (rect().bottom() + 1 - (*it)->sizeHint().height())/2); } setStyleSheet(QString("QLineEdit { padding-right: %1px; %2 }") .arg(sz.width() + frameWidth + 1) .arg(_style)); } private: typedef QList Buttons; Buttons _buttons; QString _style; }; //! @} #endif