MatchFilterComboBox.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002  * Copyright (C) 2015 by Ralf Kaestner                                        *
00003  * ralf.kaestner@gmail.com                                                    *
00004  *                                                                            *
00005  * This program is free software; you can redistribute it and/or modify       *
00006  * it under the terms of the Lesser GNU General Public License as published by*
00007  * the Free Software Foundation; either version 3 of the License, or          *
00008  * (at your option) any later version.                                        *
00009  *                                                                            *
00010  * This program is distributed in the hope that it will be useful,            *
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the               *
00013  * Lesser GNU General Public License for more details.                        *
00014  *                                                                            *
00015  * You should have received a copy of the Lesser GNU General Public License   *
00016  * along with this program. If not, see <http://www.gnu.org/licenses/>.       *
00017  ******************************************************************************/
00018 
00019 #include <QAbstractItemView>
00020 #include <QKeyEvent>
00021 #include <QLineEdit>
00022 
00023 #include "rqt_multiplot/MatchFilterComboBox.h"
00024 
00025 namespace rqt_multiplot {
00026 
00027 /*****************************************************************************/
00028 /* Constructors and Destructor                                               */
00029 /*****************************************************************************/
00030 
00031 MatchFilterComboBox::MatchFilterComboBox(QWidget* parent) :
00032   QComboBox(parent),
00033   matchFilterCompleter_(new MatchFilterCompleter(this, Qt::MatchContains)) {
00034   connect(matchFilterCompleter_, SIGNAL(activated(const QString&)), this,
00035     SLOT(matchFilterCompleterActivated(const QString&)));
00036 }
00037 
00038 MatchFilterComboBox::~MatchFilterComboBox() {
00039 }
00040 
00041 /*****************************************************************************/
00042 /* Accessors                                                                 */
00043 /*****************************************************************************/
00044 
00045 void MatchFilterComboBox::setEditable(bool editable) {
00046   if (editable != QComboBox::isEditable()) {
00047     QComboBox::setEditable(editable);
00048     
00049     if (lineEdit()) {
00050       matchFilterCompleter_->setModel(model());
00051       matchFilterCompleter_->setWidget(this);
00052       
00053       connect(lineEdit(), SIGNAL(editingFinished()), this,
00054         SLOT(lineEditEditingFinished()));
00055     }
00056     else
00057       matchFilterCompleter_->setModel(model());
00058   }
00059 }
00060 
00061 MatchFilterCompleter* MatchFilterComboBox::getMatchFilterCompleter() const {
00062   return matchFilterCompleter_;
00063 }
00064 
00065 /*****************************************************************************/
00066 /* Methods                                                                   */
00067 /*****************************************************************************/
00068 
00069 void MatchFilterComboBox::keyPressEvent(QKeyEvent* event) {
00070   bool doComplete = (count() >= 0);
00071   
00072   if (matchFilterCompleter_->popup()->isVisible()) {
00073     switch (event->key()) {
00074       case Qt::Key_Escape:
00075       case Qt::Key_Tab:
00076       case Qt::Key_Backtab:
00077         event->ignore();
00078         return;
00079       case Qt::Key_Enter:
00080       case Qt::Key_Return:
00081         if (matchFilterCompleter_->popup()->currentIndex().isValid()) {
00082           event->ignore();
00083           return; 
00084         }
00085         else {
00086           matchFilterCompleter_->popup()->hide();    
00087           doComplete = false;
00088         }
00089     }
00090   }
00091 
00092   bool isShortcut = (event->modifiers() & Qt::ControlModifier) &&
00093     (event->key() == Qt::Key_E);
00094   bool ctrlOrShift = event->modifiers() &
00095     (Qt::ControlModifier | Qt::ShiftModifier);
00096     
00097   if (!isShortcut)
00098     QComboBox::keyPressEvent(event);
00099 
00100   if (!isShortcut && !ctrlOrShift && (event->modifiers() != Qt::NoModifier)) {
00101     matchFilterCompleter_->popup()->hide();    
00102     return;
00103   }
00104 
00105   if (doComplete) {
00106     matchFilterCompleter_->setCompletionPrefix(currentText());
00107     matchFilterCompleter_->complete();
00108     matchFilterCompleter_->popup()->setCurrentIndex(QModelIndex());
00109   }
00110 }
00111 
00112 /*****************************************************************************/
00113 /* Slots                                                                     */
00114 /*****************************************************************************/
00115 
00116 void MatchFilterComboBox::matchFilterCompleterActivated(const QString& text) {  
00117   setEditText(text);
00118   lineEdit()->selectAll();
00119 
00120   setCurrentIndex(findText(text));
00121   
00122   matchFilterCompleter_->popup()->hide();
00123 }
00124 
00125 void MatchFilterComboBox::lineEditEditingFinished() {
00126   if (!matchFilterCompleter_->popup()->isVisible()) {
00127     int index = findText(currentText());
00128     
00129     if (index < 0)
00130       setEditText(currentText());
00131     else
00132       setCurrentIndex(index);
00133   }
00134   else
00135     matchFilterCompleter_->popup()->hide();
00136 }
00137 
00138 }


rqt_multiplot
Author(s): Ralf Kaestner
autogenerated on Thu Jun 6 2019 21:49:11