"Fossies" - the Fresh Open Source Software Archive

Member "seafile-client-7.0.4/src/ui/search-bar.cpp" (19 Nov 2019, 2426 Bytes) of package /linux/www/seafile-client-7.0.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "search-bar.cpp" see the Fossies "Dox" file reference documentation.

    1 #include <QToolButton>
    2 #include <QLabel>
    3 
    4 #include "search-bar.h"
    5 
    6 namespace {
    7 
    8 const int kMarginRightSearchBar = 16;
    9 const int kMarginBottom = 5;
   10 const int kHMargin = 10;
   11 
   12 } // namespace
   13 
   14 SearchBar::SearchBar(QWidget *parent)
   15     : QLineEdit(parent)
   16 {
   17     setObjectName("mSearchBar");
   18 
   19     // This property was introduced in Qt 5.2.
   20 #if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0))
   21     setClearButtonEnabled(false);
   22 #endif
   23 #ifdef Q_OS_MAC
   24     setAttribute(Qt::WA_MacShowFocusRect, 0);
   25 #endif
   26 
   27     placeholder_label_ = new QLabel(this);
   28 
   29     clear_button_ = new QToolButton(this);
   30     QIcon icon(":/images/cancel.png");
   31     clear_button_size_ = 12;
   32     clear_button_->setIcon(icon);
   33     clear_button_->setIconSize(QSize(clear_button_size_, clear_button_size_));
   34     clear_button_->setCursor(Qt::ArrowCursor);
   35     clear_button_->setStyleSheet("QToolButton { border: 0px; }");
   36     clear_button_->hide();
   37     connect(clear_button_, SIGNAL(clicked()),
   38             this, SLOT(clear()));
   39 
   40     connect(this, SIGNAL(textChanged(const QString&)),
   41             this, SLOT(onTextChanged(const QString&)));
   42 
   43     const QString style = QString(" QLineEdit#mSearchBar { "
   44                                       " padding-right: %1px; "
   45                                       " padding-left: %2px; } " )
   46                                   .arg(clear_button_size_ + kHMargin)
   47                                   .arg(kHMargin);
   48     setStyleSheet(style);
   49 }
   50 
   51 void SearchBar::paintEvent(QPaintEvent* event)
   52 {
   53     QLineEdit::paintEvent(event);
   54 }
   55 
   56 void SearchBar::resizeEvent(QResizeEvent* event)
   57 {
   58     clear_button_->move(rect().right() - kMarginRightSearchBar
   59                         - kHMargin - clear_button_size_ - 12,
   60                         (rect().bottom() - clear_button_size_) / 2);
   61     int label_height = placeholder_label_->height();
   62     int label_width = placeholder_label_->width();
   63     placeholder_label_->move((rect().right() - label_width) / 2,
   64                              (rect().bottom()  -  label_height) / 2);
   65 }
   66 
   67 void SearchBar::onTextChanged(const QString& text)
   68 {
   69     clear_button_->setVisible(!text.isEmpty());
   70     placeholder_label_->setVisible(text.isEmpty());
   71 }
   72 
   73 void SearchBar::setPlaceholderText(const QString& text)
   74 {
   75     placeholder_label_->setText(text);
   76     placeholder_label_->setStyleSheet("QLabel { font-size: 13px;"
   77                                               " color: #AAAAAA; }");
   78     placeholder_label_->show();
   79 }