![]() |
Mixxx
|
00001 #include "wwidget.h" 00002 #include "wskincolor.h" 00003 #include "wsearchlineedit.h" 00004 00005 #include <QtDebug> 00006 #include <QStyle> 00007 #include <QFont> 00008 00009 WSearchLineEdit::WSearchLineEdit(ConfigObject<ConfigValue>* pConfig, 00010 QWidget* pParent) : QLineEdit(pParent) { 00011 00012 QString skinpath = pConfig->getConfigPath(); 00013 m_clearButton = new QToolButton(this); 00014 QPixmap pixmap(skinpath.append("/skins/cross.png")); 00015 m_clearButton->setIcon(QIcon(pixmap)); 00016 m_clearButton->setIconSize(pixmap.size()); 00017 m_clearButton->setCursor(Qt::ArrowCursor); 00018 m_clearButton->setStyleSheet("QToolButton { border: none; padding: 0px; }"); 00019 m_clearButton->hide(); 00020 00021 m_place = true; 00022 showPlaceholder(); 00023 00024 QShortcut *setFocusShortcut = new QShortcut(QKeySequence(tr("Ctrl+F", "Search|Focus")), this); 00025 connect(setFocusShortcut, SIGNAL(activated()), this, SLOT(setFocus())); 00026 QShortcut *clearTextShortcut = new QShortcut(QKeySequence(tr("Esc", "Search|Clear")), this, 0, 0, Qt::WidgetShortcut); 00027 connect(clearTextShortcut, SIGNAL(activated()), this, SLOT(clear())); 00028 00029 //Set up a timer to search after a few hundred milliseconds timeout. 00030 //This stops us from thrashing the database if you type really fast. 00031 m_searchTimer.setSingleShot(true); 00032 connect(&m_searchTimer, SIGNAL(timeout()), 00033 this, SLOT(triggerSearch())); 00034 00035 connect(this, SIGNAL(textChanged(const QString&)), 00036 this, SLOT(slotSetupTimer(const QString&))); 00037 00038 //When you hit enter, it will trigger the search. 00039 connect(this, SIGNAL(returnPressed()), this, SLOT(triggerSearch())); 00040 00041 connect(m_clearButton, SIGNAL(clicked()), this, SLOT(clear())); 00042 //Forces immediate update of tracktable 00043 connect(m_clearButton, SIGNAL(clicked()), this, SLOT(triggerSearch())); 00044 00045 connect(this, SIGNAL(textChanged(const QString&)), 00046 this, SLOT(updateCloseButton(const QString&))); 00047 00048 int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); 00049 setStyleSheet(QString("QLineEdit { padding-right: %1px; } "). 00050 arg(m_clearButton->sizeHint().width() + frameWidth + 1)); 00051 00052 QSize msz = minimumSizeHint(); 00053 setMinimumSize(qMax(msz.width(), 00054 m_clearButton->sizeHint().height() + frameWidth * 2 + 2), 00055 qMax(msz.height(), 00056 m_clearButton->sizeHint().height() + frameWidth * 2 + 2)); 00057 } 00058 00059 WSearchLineEdit::~WSearchLineEdit() { 00060 } 00061 00062 void WSearchLineEdit::setup(QDomNode node) 00063 { 00064 // Background color 00065 QColor bgc(255,255,255); 00066 if (!WWidget::selectNode(node, "BgColor").isNull()) { 00067 bgc.setNamedColor(WWidget::selectNodeQString(node, "BgColor")); 00068 setAutoFillBackground(true); 00069 } 00070 QPalette pal = palette(); 00071 pal.setBrush(backgroundRole(), WSkinColor::getCorrectColor(bgc)); 00072 00073 // Foreground color 00074 m_fgc = QColor(0,0,0); 00075 if (!WWidget::selectNode(node, "FgColor").isNull()) { 00076 m_fgc.setNamedColor(WWidget::selectNodeQString(node, "FgColor")); 00077 } 00078 bgc = WSkinColor::getCorrectColor(bgc); 00079 m_fgc = QColor(255 - bgc.red(), 255 - bgc.green(), 255 - bgc.blue()); 00080 pal.setBrush(foregroundRole(), m_fgc); 00081 setPalette(pal); 00082 00083 } 00084 00085 void WSearchLineEdit::resizeEvent(QResizeEvent* e) { 00086 QLineEdit::resizeEvent(e); 00087 QSize sz = m_clearButton->sizeHint(); 00088 int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); 00089 m_clearButton->move(rect().right() - frameWidth - sz.width(), 00090 (rect().bottom() + 1 - sz.height())/2); 00091 } 00092 00093 void WSearchLineEdit::focusInEvent(QFocusEvent* event) { 00094 QLineEdit::focusInEvent(event); 00095 if (m_place) { 00096 //Must block signals here so that we don't emit a search() signal via 00097 //textChanged(). 00098 blockSignals(true); 00099 setText(""); 00100 blockSignals(false); 00101 QPalette pal = palette(); 00102 pal.setColor(foregroundRole(), m_fgc); 00103 setPalette(pal); 00104 m_place = false; 00105 emit(searchStarting()); 00106 } 00107 } 00108 00109 void WSearchLineEdit::focusOutEvent(QFocusEvent* event) { 00110 QLineEdit::focusOutEvent(event); 00111 if (text().isEmpty()) { 00112 m_place = true; 00113 showPlaceholder(); 00114 emit(searchCleared()); 00115 } else { 00116 m_place = false; 00117 } 00118 } 00119 00120 void WSearchLineEdit::restoreSearch(const QString& text) { 00121 qDebug() << "WSearchLineEdit::restoreSearch(" << text << ")"; 00122 blockSignals(true); 00123 setText(text); 00124 blockSignals(false); 00125 if (text == "") { 00126 m_place = true; 00127 showPlaceholder(); 00128 } else { 00129 QPalette pal = palette(); 00130 pal.setColor(foregroundRole(), m_fgc); 00131 setPalette(pal); 00132 m_place = false; 00133 } 00134 updateCloseButton(text); 00135 } 00136 00137 void WSearchLineEdit::slotSetupTimer(const QString& text) 00138 { 00139 m_searchTimer.stop(); 00140 //300 milliseconds timeout 00141 m_searchTimer.start(300); 00142 } 00143 00144 void WSearchLineEdit::triggerSearch() 00145 { 00146 m_searchTimer.stop(); 00147 emit(search(text())); 00148 } 00149 00150 void WSearchLineEdit::showPlaceholder() { 00151 //Must block signals here so that we don't emit a search() signal via 00152 //textChanged(). 00153 blockSignals(true); 00154 setText(tr("Search...")); 00155 blockSignals(false); 00156 QPalette pal = palette(); 00157 pal.setColor(foregroundRole(), Qt::lightGray); 00158 setPalette(pal); 00159 } 00160 00161 void WSearchLineEdit::updateCloseButton(const QString& text) 00162 { 00163 m_clearButton->setVisible(!text.isEmpty() && !m_place); 00164 }