PageRenderTime 181ms CodeModel.GetById 80ms app.highlight 14ms RepoModel.GetById 67ms app.codeStats 0ms

/src/libtomahawk/thirdparty/Qocoa/qsearchfield.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 170 lines | 113 code | 33 blank | 24 comment | 11 complexity | 24d14db8bc360036cf250403f0edaeab MD5 | raw file
  1/*
  2Copyright (C) 2011 by Mike McQuaid
  3Copyright (C) 2011 by Leo Franchi
  4
  5Permission is hereby granted, free of charge, to any person obtaining a copy
  6of this software and associated documentation files (the "Software"), to deal
  7in the Software without restriction, including without limitation the rights
  8to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9copies of the Software, and to permit persons to whom the Software is
 10furnished to do so, subject to the following conditions:
 11
 12The above copyright notice and this permission notice shall be included in
 13all copies or substantial portions of the Software.
 14
 15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 21THE SOFTWARE.
 22*/
 23
 24#include "qsearchfield.h"
 25
 26#include "widgets/searchlineedit/SearchLineEdit.h"
 27#include "utils/TomahawkUtilsGui.h"
 28
 29#include <QLineEdit>
 30#include <QVBoxLayout>
 31#include <QEvent>
 32
 33class DLLEXPORT QSearchFieldPrivate : public QObject
 34{
 35    Q_OBJECT
 36public:
 37    QSearchFieldPrivate(SearchLineEdit *lineEdit) : QObject( lineEdit ), lineEdit(lineEdit) {}
 38    virtual ~QSearchFieldPrivate() {}
 39
 40    SearchLineEdit *lineEdit;
 41};
 42
 43QSearchField::QSearchField(QWidget *parent) : QWidget(parent)
 44{
 45    SearchLineEdit *lineEdit = new SearchLineEdit(this);
 46    connect(lineEdit, SIGNAL(textChanged(QString)),
 47            this, SIGNAL(textChanged(QString)));
 48    connect(lineEdit, SIGNAL(returnPressed()),
 49            this, SIGNAL(returnPressed()));
 50
 51    pimpl = QPointer< QSearchFieldPrivate>( new QSearchFieldPrivate( lineEdit ) );
 52
 53    QVBoxLayout *layout = new QVBoxLayout(this);
 54    layout->addWidget(lineEdit);
 55    TomahawkUtils::unmarginLayout(layout);
 56    setContentsMargins(0, 0, 0, 0);
 57
 58    lineEdit->setStyleSheet( "QLineEdit { border: 1px solid gray; border-radius: 6px; }" );
 59    lineEdit->setMinimumHeight(27);
 60    setFixedHeight(27);
 61
 62#ifdef Q_OS_MAC
 63    lineEdit->setContentsMargins(0, 0, 0, 0);
 64#else
 65    lineEdit->setContentsMargins(2, 2, 2, 2);
 66#endif
 67
 68}
 69
 70void QSearchField::setText(const QString &text)
 71{
 72    Q_ASSERT( !pimpl.isNull() );
 73    if ( pimpl.isNull() )
 74        return;
 75
 76    pimpl.data()->lineEdit->setText(text);
 77}
 78
 79void QSearchField::setPlaceholderText(const QString& text)
 80{
 81    Q_ASSERT( !pimpl.isNull() );
 82    if ( pimpl.isNull() )
 83        return;
 84
 85    pimpl.data()->lineEdit->setInactiveText( text );
 86}
 87
 88void QSearchField::clear()
 89{
 90    Q_ASSERT( !pimpl.isNull() );
 91    if ( pimpl.isNull() )
 92        return;
 93
 94    pimpl.data()->lineEdit->clear();
 95}
 96
 97QString QSearchField::text() const
 98{
 99    Q_ASSERT( !pimpl.isNull() );
100    if ( pimpl.isNull() )
101        return QString();
102
103    return pimpl.data()->lineEdit->text();
104}
105
106QString QSearchField::placeholderText() const
107{
108    Q_ASSERT( !pimpl.isNull() );
109    if ( pimpl.isNull() )
110        return QString();
111
112    return pimpl.data()->lineEdit->placeholderText();
113}
114
115void QSearchField::selectAll()
116{
117    Q_ASSERT( !pimpl.isNull() );
118    if ( pimpl.isNull() )
119        return;
120
121    pimpl.data()->lineEdit->selectAll();
122}
123
124void QSearchField::setFocus()
125{
126    Q_ASSERT( !pimpl.isNull() );
127    if ( pimpl.isNull() )
128        return;
129
130    pimpl.data()->lineEdit->setFocus();
131}
132
133void QSearchField::setFocus(Qt::FocusReason reason)
134{
135    Q_ASSERT( !pimpl.isNull() );
136    if ( pimpl.isNull() )
137        return;
138
139    pimpl.data()->lineEdit->setFocus(reason);
140}
141
142void QSearchField::setMenu(QMenu *menu)
143{
144    //FIXME nop
145}
146
147void QSearchField::popupMenu()
148{
149    // noop
150}
151
152void QSearchField::changeEvent(QEvent* event)
153{
154    if (event->type() == QEvent::EnabledChange) {
155        Q_ASSERT(pimpl);
156        if (!pimpl)
157            return;
158
159        const bool enabled = isEnabled();
160        pimpl->lineEdit->setEnabled(enabled);
161    }
162    QWidget::changeEvent(event);
163}
164
165void QSearchField::resizeEvent(QResizeEvent* e)
166{
167    QWidget::resizeEvent(e);
168}
169
170#include "qsearchfield.moc"