diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2008-09-28 23:34:32 -0400 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2008-09-28 23:34:32 -0400 |
commit | 87df51788bb3cd48e0194369227c6cbd16e39802 (patch) | |
tree | 19260bfbd050216408ed2268310655667584b63e | |
parent | Upgraded to new authentication scheme and fixed header includes. (diff) | |
download | zmusicplayer-87df51788bb3cd48e0194369227c6cbd16e39802.tar.xz zmusicplayer-87df51788bb3cd48e0194369227c6cbd16e39802.zip |
Changed search bar implementation. Fixed scroller. Changed include types for better kde4 integration. Style changes.
-rw-r--r-- | AutoSizingList.cpp | 4 | ||||
-rw-r--r-- | Credentials.cpp | 8 | ||||
-rw-r--r-- | PlayerWindow.cpp | 9 | ||||
-rw-r--r-- | PlayerWindow.h | 9 | ||||
-rw-r--r-- | SearchLineEdit.cpp | 304 | ||||
-rw-r--r-- | SearchLineEdit.h | 223 |
6 files changed, 371 insertions, 186 deletions
diff --git a/AutoSizingList.cpp b/AutoSizingList.cpp index 5899f75..85e4dd1 100644 --- a/AutoSizingList.cpp +++ b/AutoSizingList.cpp @@ -89,7 +89,7 @@ void AutoSizingList::fixColumnSizes() setColumnWidth(2, m_suggestedRatio2 * width); setColumnWidth(3, m_suggestedRatio3 * width); } - QModelIndexList indexes = selectedIndexes(); + /*QModelIndexList indexes = selectedIndexes(); if(indexes.count() > 0 && indexes[0].isValid()) scrollTo(indexes[0], QAbstractItemView::PositionAtCenter); else { @@ -99,7 +99,7 @@ void AutoSizingList::fixColumnSizes() if(index.isValid()) scrollTo(filter->mapFromSource(index), QAbstractItemView::PositionAtCenter); } - } + }*/ } void AutoSizingList::resizeEvent(QResizeEvent *event) { diff --git a/Credentials.cpp b/Credentials.cpp index c1e93f6..f7c16e5 100644 --- a/Credentials.cpp +++ b/Credentials.cpp @@ -139,13 +139,13 @@ void Credentials::downloadFinished() song->setSha1(tagElement.text()); } else if(tagElement.tagName() == "artist") { song->setArtist(tagElement.text()); - } else if(tagElement.tagName() == "album") { + } else if(tagElement.tagName() == "album") { song->setAlbum(tagElement.text()); - } else if(tagElement.tagName() == "title") { + } else if(tagElement.tagName() == "title") { song->setTitle(tagElement.text()); - } else if(tagElement.tagName() == "track") { + } else if(tagElement.tagName() == "track") { song->setTrack(tagElement.text().toInt()); - } else if(tagElement.tagName() == "format") { + } else if(tagElement.tagName() == "format") { song->setFormat(tagElement.text()); } } diff --git a/PlayerWindow.cpp b/PlayerWindow.cpp index 8ec680e..22efb57 100644 --- a/PlayerWindow.cpp +++ b/PlayerWindow.cpp @@ -19,7 +19,6 @@ #include <QVBoxLayout> #include <QSortFilterProxyModel> #include <QPushButton> -//#include <AudioOutput> //BEGIN CONSTRUCT OBJECTS PlayerWindow::PlayerWindow(QWidget *parent, Qt::WindowFlags flags) @@ -47,11 +46,8 @@ void PlayerWindow::setupUi() m_stopAction->setDisabled(true); m_nextAction->setDisabled(true); m_previousAction->setDisabled(true); - //TODO: change to volumemediamute on 0 volume m_volumeSlider = new Phonon::VolumeSlider(this); m_volumeSlider->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred); - QLabel *volumeLabel = new QLabel(this); - volumeLabel->setPixmap(style()->standardIcon(QStyle::SP_MediaVolume).pixmap(16)); m_seekSlider = new Phonon::SeekSlider(this); m_seekSlider->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); @@ -62,7 +58,6 @@ void PlayerWindow::setupUi() toolBar->addAction(m_stopAction); toolBar->addAction(m_nextAction); toolBar->addWidget(m_seekSlider); - toolBar->addWidget(volumeLabel); toolBar->addWidget(m_volumeSlider); toolBar->setIconSize(QSize(16, 16)); toolBar->setMovable(false); @@ -253,7 +248,7 @@ void PlayerWindow::searchChanged(const QString&) void PlayerWindow::updateSearch() { if(m_filter) { - m_filter->setFilter(m_searchEdit->lineEdit()->text().trimmed()); + m_filter->setFilter(m_searchEdit->text().trimmed()); updateControls(); } } @@ -262,7 +257,7 @@ void PlayerWindow::moveToCurrent() if(m_filter) { QModelIndex index = m_filter->currentItem(); if(!m_filter->mapFromSource(index).isValid()) { - m_searchEdit->lineEdit()->clear(); + m_searchEdit->clear(); updateSearch(); } m_treeView->scrollTo(m_filter->mapFromSource(index), QAbstractItemView::PositionAtCenter); diff --git a/PlayerWindow.h b/PlayerWindow.h index cddb948..6281a07 100644 --- a/PlayerWindow.h +++ b/PlayerWindow.h @@ -8,10 +8,11 @@ #include <QMainWindow> #include <QTimer> #include <QModelIndex> -//#include <MediaObject> -//#include <SeekSlider> -//#include <VolumeSlider> -#include <phonon> +#include <MediaObject> +#include <SeekSlider> +#include <VolumeSlider> +#include <AudioOutput> +//#include <phonon> class Song; class QBuffer; diff --git a/SearchLineEdit.cpp b/SearchLineEdit.cpp index eda7c39..0b7f99e 100644 --- a/SearchLineEdit.cpp +++ b/SearchLineEdit.cpp @@ -1,6 +1,5 @@ /* * Copyright 2008 Benjamin C. Meyer <ben@meyerhome.net> - * Taken from Arora Browser. */ #include "SearchLineEdit.h" @@ -9,167 +8,186 @@ #include <qmenu.h> #include <qpainter.h> #include <qstyle.h> -#include <qcompleter.h> +#include <qstyleoption.h> +#include <qdebug.h> +#include <qlayout.h> - -ExLineEdit::ExLineEdit(QWidget *parent) +SideWidget::SideWidget(QWidget *parent) : QWidget(parent) - , m_leftWidget(0) - , m_lineEdit(new QLineEdit(this)) - , m_clearButton(0) { - setFocusPolicy(m_lineEdit->focusPolicy()); - setAttribute(Qt::WA_InputMethodEnabled); - setSizePolicy(m_lineEdit->sizePolicy()); - setBackgroundRole(m_lineEdit->backgroundRole()); - setMouseTracking(true); - setAcceptDrops(true); - setAttribute(Qt::WA_MacShowFocusRect, true); - QPalette p = m_lineEdit->palette(); - setPalette(p); - - // line edit - m_lineEdit->setFrame(false); - m_lineEdit->setFocusProxy(this); - m_lineEdit->setAttribute(Qt::WA_MacShowFocusRect, false); - QPalette clearPalette = m_lineEdit->palette(); - clearPalette.setBrush(QPalette::Base, QBrush(Qt::transparent)); - m_lineEdit->setPalette(clearPalette); - - // clearButton - m_clearButton = new ClearButton(this); - connect(m_clearButton, SIGNAL(clicked()), - m_lineEdit, SLOT(clear())); - connect(m_lineEdit, SIGNAL(textChanged(const QString&)), - m_clearButton, SLOT(textChanged(const QString&))); } -void ExLineEdit::setLeftWidget(QWidget *widget) +bool SideWidget::event(QEvent *event) { - m_leftWidget = widget; + if (event->type() == QEvent::LayoutRequest) + emit sizeHintChanged(); + return QWidget::event(event); } -QWidget *ExLineEdit::leftWidget() const +LineEdit::LineEdit(QWidget *parent) + : QLineEdit(parent) + , m_leftLayout(0) + , m_rightLayout(0) { - return m_leftWidget; +#if QT_VERSION < 0x040500 + setStyle(new LineEditStyle); +#endif + init(); } -void ExLineEdit::resizeEvent(QResizeEvent *event) +LineEdit::LineEdit(const QString &contents, QWidget *parent) + : QLineEdit(contents, parent) + , m_leftLayout(0) + , m_rightLayout(0) { - Q_ASSERT(m_leftWidget); - updateGeometries(); - QWidget::resizeEvent(event); + init(); } -void ExLineEdit::updateGeometries() +void LineEdit::init() { - QStyleOptionFrameV2 panel; - initStyleOption(&panel); - QRect rect = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this); - - int height = rect.height(); - int width = rect.width(); - - int m_leftWidgetHeight = m_leftWidget->height(); - int clearButtonWidth = this->height(); - - switch (layoutDirection()) { - case Qt::RightToLeft: - m_clearButton->setGeometry(rect.x(), rect.y() + (height - clearButtonWidth) / 2, - clearButtonWidth, this->height()); - - m_lineEdit->setGeometry(m_clearButton->x() + m_clearButton->width(), 0, - width - clearButtonWidth - m_leftWidget->width() - 2, this->height()); - - m_leftWidget->setGeometry(this->width() - m_leftWidget->width() - 2, rect.y() + (height - m_leftWidgetHeight) / 2, - m_leftWidget->width(), m_leftWidget->height()); - - break; - - case Qt::LeftToRight: - default: - m_leftWidget->setGeometry(rect.x() + 2,rect.y() + (height - m_leftWidgetHeight) / 2, - m_leftWidget->width(), m_leftWidget->height()); - - m_lineEdit->setGeometry(m_leftWidget->x() + m_leftWidget->width(), 0, - width - clearButtonWidth - m_leftWidget->width(), this->height()); + m_leftWidget = new SideWidget(this); + m_leftWidget->resize(0, 0); + m_leftLayout = new QHBoxLayout(m_leftWidget); + m_leftLayout->setContentsMargins(0, 0, 0, 0); + if (isRightToLeft()) + m_leftLayout->setDirection(QBoxLayout::RightToLeft); + else + m_leftLayout->setDirection(QBoxLayout::LeftToRight); + m_leftLayout->setSizeConstraint(QLayout::SetFixedSize); + + m_rightWidget = new SideWidget(this); + m_rightWidget->resize(0, 0); + m_rightLayout = new QHBoxLayout(m_rightWidget); + if (isRightToLeft()) + m_rightLayout->setDirection(QBoxLayout::RightToLeft); + else + m_rightLayout->setDirection(QBoxLayout::LeftToRight); + m_rightLayout->setContentsMargins(0, 0, 0, 0); + + QSpacerItem *horizontalSpacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum); + m_rightLayout->addItem(horizontalSpacer); + + setWidgetSpacing(3); + connect(m_leftWidget, SIGNAL(sizeHintChanged()), + this, SLOT(updateTextMargins())); + connect(m_rightWidget, SIGNAL(sizeHintChanged()), + this, SLOT(updateTextMargins())); +} - m_clearButton->setGeometry(this->width() - clearButtonWidth, 0, - clearButtonWidth, this->height()); - break; +bool LineEdit::event(QEvent *event) +{ + if (event->type() == QEvent::LayoutDirectionChange) { + if (isRightToLeft()) { + m_leftLayout->setDirection(QBoxLayout::RightToLeft); + m_rightLayout->setDirection(QBoxLayout::RightToLeft); + } else { + m_leftLayout->setDirection(QBoxLayout::LeftToRight); + m_rightLayout->setDirection(QBoxLayout::LeftToRight); + } } + return QLineEdit::event(event); } -void ExLineEdit::initStyleOption(QStyleOptionFrameV2 *option) const +void LineEdit::addWidget(QWidget *widget, WidgetPosition position) { - option->initFrom(this); - option->rect = contentsRect(); - option->lineWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, option, this); - option->midLineWidth = 0; - option->state |= QStyle::State_Sunken; - if (m_lineEdit->isReadOnly()) - option->state |= QStyle::State_ReadOnly; -#ifdef QT_KEYPAD_NAVIGATION - if (hasEditFocus()) - option->state |= QStyle::State_HasEditFocus; -#endif - option->features = QStyleOptionFrameV2::None; + if (!widget) + return; + + bool rtl = isRightToLeft(); + if (rtl) + position = (position == LeftSide) ? RightSide : LeftSide; + widget->show(); + if (position == LeftSide) { + m_leftLayout->addWidget(widget); + } else { + m_rightLayout->insertWidget(1, widget); + } } -QSize ExLineEdit::sizeHint() const +void LineEdit::removeWidget(QWidget *widget) { - m_lineEdit->setFrame(true); - QSize size = m_lineEdit->sizeHint(); - m_lineEdit->setFrame(false); - return size; + if (!widget) + return; + + m_leftLayout->removeWidget(widget); + m_rightLayout->removeWidget(widget); + widget->hide(); } -void ExLineEdit::focusInEvent(QFocusEvent *event) +void LineEdit::setWidgetSpacing(int spacing) { - m_lineEdit->event(event); - QWidget::focusInEvent(event); + m_leftLayout->setSpacing(spacing); + m_rightLayout->setSpacing(spacing); + updateTextMargins(); } -void ExLineEdit::focusOutEvent(QFocusEvent *event) +int LineEdit::widgetSpacing() const { - m_lineEdit->event(event); - - if (m_lineEdit->completer()) { - connect(m_lineEdit->completer(), SIGNAL(activated(QString)), - m_lineEdit, SLOT(setText(QString))); - connect(m_lineEdit->completer(), SIGNAL(highlighted(QString)), - m_lineEdit, SLOT(_q_completionHighlighted(QString))); - } - QWidget::focusOutEvent(event); + return m_leftLayout->spacing(); } -void ExLineEdit::keyPressEvent(QKeyEvent *event) +int LineEdit::textMargin(WidgetPosition position) const { - m_lineEdit->event(event); - QWidget::keyPressEvent(event); + int spacing = m_rightLayout->spacing(); + int w = 0; + if (position == LeftSide) + w = m_leftWidget->sizeHint().width(); + else + w = m_rightWidget->sizeHint().width(); + if (w == 0) + return 0; + return w + spacing * 2; } -bool ExLineEdit::event(QEvent *event) +void LineEdit::updateTextMargins() { - if (event->type() == QEvent::ShortcutOverride) - m_lineEdit->event(event); - return QWidget::event(event); +#if QT_VERSION >= 0x040500 + int left = textMargin(LineEdit::LeftSide); + int right = textMargin(LineEdit::RightSide); + int top = 0; + int bottom = 0; + setTextMargins(left, top, right, bottom); +#else + update(); +#endif + updateSideWidgetLocations(); } -void ExLineEdit::paintEvent(QPaintEvent *) +void LineEdit::updateSideWidgetLocations() { - QPainter p(this); - QStyleOptionFrameV2 panel; - initStyleOption(&panel); - style()->drawPrimitive(QStyle::PE_PanelLineEdit, &panel, &p, this); + QStyleOptionFrameV2 opt; + initStyleOption(&opt); + QRect textRect = style()->subElementRect(QStyle::SE_LineEditContents, &opt, this); + int spacing = m_rightLayout->spacing(); + textRect.adjust(spacing, 0, -spacing, 0); + + int left = textMargin(LineEdit::LeftSide); +#if QT_VERSION < 0x040500 + int right = textMargin(LineEdit::RightSide); + textRect.adjust(-left, 1, right, 0); +#endif + + int midHeight = textRect.center().y(); + + if (m_leftLayout->count() > 0) { + int leftHeight = midHeight - m_leftWidget->height() / 2; + int leftWidth = m_leftWidget->width(); + if (leftWidth == 0) + leftHeight = midHeight - m_leftWidget->sizeHint().height() / 2; + m_leftWidget->move(textRect.x(), leftHeight); + } + textRect.setX(left); + textRect.setY(midHeight - m_rightWidget->sizeHint().height() / 2); + textRect.setHeight(m_rightWidget->sizeHint().height()); + m_rightWidget->setGeometry(textRect); } -void ExLineEdit::clear() +void LineEdit::resizeEvent(QResizeEvent *event) { - m_lineEdit->clear(); + updateSideWidgetLocations(); + QLineEdit::resizeEvent(event); } - ClearButton::ClearButton(QWidget *parent) : QAbstractButton(parent) { @@ -177,6 +195,7 @@ ClearButton::ClearButton(QWidget *parent) setToolTip(tr("Clear")); setVisible(false); setFocusPolicy(Qt::NoFocus); + setMinimumSize(22, 22); } void ClearButton::paintEvent(QPaintEvent *event) @@ -287,41 +306,54 @@ void SearchButton::paintEvent(QPaintEvent *event) - When there is text a clear button is displayed on the right hand side */ SearchLineEdit::SearchLineEdit(QWidget *parent) - : ExLineEdit(parent) - , m_searchButton(new SearchButton(this)) + : LineEdit(parent) + , m_searchButton(0) { - connect(lineEdit(), SIGNAL(textChanged(const QString &)), - this, SIGNAL(textChanged(const QString &))); - setLeftWidget(m_searchButton); + setUpdatesEnabled(false); + m_searchButton = new SearchButton(this); + updateGeometries(); + addWidget(m_searchButton, LeftSide); m_inactiveText = tr("Search"); QSizePolicy policy = sizePolicy(); setSizePolicy(QSizePolicy::Preferred, policy.verticalPolicy()); + + // clear button on the right + ClearButton *m_clearButton = new ClearButton(this); + connect(m_clearButton, SIGNAL(clicked()), + this, SLOT(clear())); + connect(this, SIGNAL(textChanged(const QString&)), + m_clearButton, SLOT(textChanged(const QString&))); + addWidget(m_clearButton, RightSide); + m_clearButton->hide(); + updateTextMargins(); + setUpdatesEnabled(true); } void SearchLineEdit::paintEvent(QPaintEvent *event) { - if (lineEdit()->text().isEmpty() && !hasFocus() && !m_inactiveText.isEmpty()) { - ExLineEdit::paintEvent(event); + if (text().isEmpty() && !hasFocus() && !m_inactiveText.isEmpty()) { + LineEdit::paintEvent(event); QStyleOptionFrameV2 panel; initStyleOption(&panel); - QRect r = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this); - QFontMetrics fm = fontMetrics(); - int horizontalMargin = lineEdit()->x(); - QRect lineRect(horizontalMargin + r.x(), r.y() + (r.height() - fm.height() + 1) / 2, - r.width() - 2 * horizontalMargin, fm.height()); + QRect textRect = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this); +#if QT_VERSION >= 0x040500 + int left = textMargin(LineEdit::LeftSide); + int right = textMargin(LineEdit::RightSide); + textRect.adjust(left, 0, -right, 0); +#endif QPainter painter(this); painter.setPen(palette().brush(QPalette::Disabled, QPalette::Text).color()); - painter.drawText(lineRect, Qt::AlignLeft | Qt::AlignVCenter, m_inactiveText); + painter.drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, m_inactiveText); } else { - ExLineEdit::paintEvent(event); + LineEdit::paintEvent(event); } } void SearchLineEdit::resizeEvent(QResizeEvent *event) { updateGeometries(); - ExLineEdit::resizeEvent(event); + LineEdit::resizeEvent(event); } void SearchLineEdit::updateGeometries() @@ -330,7 +362,9 @@ void SearchLineEdit::updateGeometries() int menuWidth = menuHeight + 1; if (!m_searchButton->m_menu) menuWidth = (menuHeight / 5) * 4; - m_searchButton->resize(QSize(menuWidth, menuHeight)); + m_searchButton->setMinimumSize(QSize(menuWidth, menuHeight)); + m_searchButton->resize(menuWidth, menuHeight); + updateTextMargins(); } QString SearchLineEdit::inactiveText() const diff --git a/SearchLineEdit.h b/SearchLineEdit.h index 01c16f4..3447aaf 100644 --- a/SearchLineEdit.h +++ b/SearchLineEdit.h @@ -1,54 +1,164 @@ /* * Copyright 2008 Benjamin C. Meyer <ben@meyerhome.net> - * Taken from Arora Browser. */ -#ifndef SEARCHLINEEDIT_H -#define SEARCHLINEEDIT_H -#include <qlineedit.h> -#include <qabstractbutton.h> -#include <qwidget.h> -#include <qstyleoption.h> +#ifndef PROXYSTYLE_H +#define PROXYSTYLE_H -QT_BEGIN_NAMESPACE -class QMenu; -class QLineEdit; -QT_END_NAMESPACE +#include <qstyle.h> +#include <qstylefactory.h> -class SearchButton; -class ClearButton; -class ExLineEdit : public QWidget +/* + A base class that can be used to write a style that modifise the currently style. + For more details see: http://doc.trolltech.com/qq/qq09-q-and-a.html#style +*/ +class ProxyStyle : public QStyle { Q_OBJECT public: - ExLineEdit(QWidget *parent = 0); + explicit ProxyStyle(const QString &baseStyle) + { + style = QStyleFactory::create(baseStyle); + if (!style) + style = QStyleFactory::create(QLatin1String("windows")); + } + ~ProxyStyle() + { delete style; } + + virtual void drawComplexControl(ComplexControl control, const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget = 0) const + { style->drawComplexControl(control, option, painter, widget); } + virtual void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = 0) const + { style->drawControl(element, option, painter, widget); } + virtual void drawItemPixmap(QPainter *painter, const QRect &rectangle, int alignment, const QPixmap &pixmap) const + { style->drawItemPixmap(painter, rectangle, alignment, pixmap); } + virtual void drawItemText(QPainter *painter, const QRect &rectangle, int alignment, const QPalette &palette, bool enabled, const QString &text, QPalette::ColorRole textRole = QPalette::NoRole) const + { style->drawItemText(painter, rectangle, alignment, palette, enabled, text, textRole); } + virtual void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = 0) const + { style->drawPrimitive(element, option, painter, widget); } + virtual QPixmap generatedIconPixmap(QIcon::Mode iconMode, const QPixmap &pixmap, const QStyleOption *option) const + { return style->generatedIconPixmap(iconMode, pixmap, option); } + virtual SubControl hitTestComplexControl(ComplexControl control, const QStyleOptionComplex *option, const QPoint &position, const QWidget *widget = 0) const + { return style->hitTestComplexControl(control, option, position, widget); } + virtual QRect itemPixmapRect(const QRect &rectangle, int alignment, const QPixmap &pixmap) const + { return style->itemPixmapRect(rectangle, alignment, pixmap); } + virtual QRect itemTextRect(const QFontMetrics &metrics, const QRect &rectangle, int alignment, bool enabled, const QString &text) const + { return style->itemTextRect(metrics, rectangle, alignment, enabled, text); } + virtual int pixelMetric(PixelMetric metric, const QStyleOption *option = 0, const QWidget *widget = 0) const + { return style->pixelMetric(metric, option, widget); } + virtual void polish(QWidget *widget) + { style->polish(widget); } + virtual void polish(QApplication *application) + { style->polish(application); } + virtual void polish(QPalette &palette) + { style->polish(palette); } + virtual QSize sizeFromContents(ContentsType type, const QStyleOption *option, const QSize &contentsSize, const QWidget *widget = 0) const + { return style->sizeFromContents(type, option, contentsSize, widget); } + virtual QPalette standardPalette () const + { return style->standardPalette(); } + virtual int styleHint(StyleHint hint, const QStyleOption *option = 0, const QWidget *widget = 0, QStyleHintReturn *returnData = 0) const + { return style->styleHint(hint, option, widget, returnData); } + virtual QRect subControlRect(ComplexControl control, const QStyleOptionComplex *option, SubControl subControl, const QWidget *widget = 0) const + { return style->subControlRect(control, option, subControl, widget); } + virtual void unpolish(QWidget *widget) + { style->unpolish(widget); } + virtual void unpolish(QApplication *application) + { style->unpolish(application); } + virtual QPixmap standardPixmap(StandardPixmap standardPixmap, const QStyleOption *opt = 0, const QWidget *widget = 0) const + { return style->standardPixmap(standardPixmap, opt, widget); } + virtual QRect subElementRect(SubElement element, const QStyleOption *option, const QWidget *widget = 0) const + { return style->subElementRect(element, option, widget); } + virtual bool event(QEvent *e) + { return style->event(e); } + virtual bool eventFilter(QObject *o, QEvent *e) + { return style->eventFilter(o, e); } + +protected slots: + int layoutSpacingImplementation ( QSizePolicy::ControlType control1, QSizePolicy::ControlType control2, Qt::Orientation orientation, const QStyleOption * option = 0, const QWidget * widget = 0 ) const + { return style->layoutSpacing(control1, control2, orientation, option, widget); } + + QIcon standardIconImplementation ( StandardPixmap standardIcon, const QStyleOption * option = 0, const QWidget * widget = 0 ) const + { return style->standardIcon(standardIcon, option, widget); } + +private: + QStyle* style; + +}; + +#endif // PROXYSTYLE_H - inline QLineEdit *lineEdit() const { return m_lineEdit; } - void setLeftWidget(QWidget *widget); - QWidget *leftWidget() const; +#ifndef LINEEDIT_H +#define LINEEDIT_H - QSize sizeHint() const; +#include <qlineedit.h> + +class QHBoxLayout; +class SideWidget; + +/* + LineEdit is a subclass of QLineEdit that provides an easy and simple + way to add widgets on the left or right hand side of the text. + + The layout of the widgets on either side are handled by a QHBoxLayout. + You can set the spacing around the widgets with setWidgetSpacing(). + + As widgets are added to the class they are inserted from the outside + into the center of the widget. +*/ +class LineEdit : public QLineEdit +{ + Q_OBJECT + +public: + enum WidgetPosition { + LeftSide, + RightSide + }; + + LineEdit(QWidget *parent = 0); + LineEdit(const QString &contents, QWidget *parent = 0); + + void addWidget(QWidget *widget, WidgetPosition position); + void removeWidget(QWidget *widget); + void setWidgetSpacing(int spacing); + int widgetSpacing() const; + int textMargin(WidgetPosition position) const; protected: - void focusInEvent(QFocusEvent *event); - void focusOutEvent(QFocusEvent *event); - void keyPressEvent(QKeyEvent *event); - void paintEvent(QPaintEvent *event); void resizeEvent(QResizeEvent *event); bool event(QEvent *event); - void clear(); -protected: - void updateGeometries(); - void initStyleOption(QStyleOptionFrameV2 *option) const; +protected slots: + void updateTextMargins(); + +private: + void init(); + void updateSideWidgetLocations(); + + SideWidget *m_leftWidget; + SideWidget *m_rightWidget; + QHBoxLayout *m_leftLayout; + QHBoxLayout *m_rightLayout; - QWidget *m_leftWidget; - QLineEdit *m_lineEdit; - ClearButton *m_clearButton; }; + +#endif // LINEEDIT_H + + +#ifndef SEARCHLINEEDIT_H +#define SEARCHLINEEDIT_H + +#include <qlineedit.h> +#include <qabstractbutton.h> + +QT_BEGIN_NAMESPACE +class QMenu; +QT_END_NAMESPACE + +class SearchButton; + /* Clear button on the right hand side of the search widget. Hidden by default @@ -67,14 +177,11 @@ public slots: }; -class SearchLineEdit : public ExLineEdit +class SearchLineEdit : public LineEdit { Q_OBJECT Q_PROPERTY(QString inactiveText READ inactiveText WRITE setInactiveText) -signals: - void textChanged(const QString &text); - public: SearchLineEdit(QWidget *parent = 0); @@ -96,4 +203,52 @@ private: }; #endif // SEARCHLINEEDIT_H +#ifndef LINEEDIT_P_H +#define LINEEDIT_P_H + +#include <qwidget.h> + +class SideWidget : public QWidget +{ + Q_OBJECT + +signals: + void sizeHintChanged(); + +public: + SideWidget(QWidget *parent = 0); + +protected: + bool event(QEvent *event); + +}; + +#if QT_VERSION < 0x040500 + +#include "qapplication.h" + +class LineEditStyle : public ProxyStyle +{ + +public: + explicit LineEditStyle() + : ProxyStyle(QApplication::style()->objectName()) { + } + + QRect subElementRect(SubElement element, const QStyleOption *option, const QWidget *widget = 0) const { + QRect r = ProxyStyle::subElementRect(element, option, widget); + if (element == SE_LineEditContents) { + if (const LineEdit *le = qobject_cast<const LineEdit *>(widget)) { + int left = le->textMargin(LineEdit::LeftSide); + int right = le->textMargin(LineEdit::RightSide); + r.adjust(left, 0, -right, 0); + } + } + return r; + } + +}; + +#endif // QT_VERSION +#endif // LINEEDIT_P_H |