|
@@ -1,176 +0,0 @@
|
|
|
-#include "searchcombobox.h"
|
|
|
-#include "wordtopinyin.h"
|
|
|
-
|
|
|
-#include <QStyleFactory>
|
|
|
-#include <QListView>
|
|
|
-#include <QApplication>
|
|
|
-#include <QGraphicsDropShadowEffect>
|
|
|
-#include <QTimer>
|
|
|
-
|
|
|
-SearchComboBox::SearchComboBox(QWidget *parent)
|
|
|
- : QComboBox(parent)
|
|
|
- , m_autoquery(false)
|
|
|
- , m_showPopup(false)
|
|
|
-{
|
|
|
- setStyle(QStyleFactory::create("Windows"));
|
|
|
-
|
|
|
- QTimer::singleShot(0, this, [=]
|
|
|
- {
|
|
|
- view()->setMinimumWidth(width() + LISTVIEW_MARGIN * 2);
|
|
|
- });
|
|
|
-
|
|
|
- setEditable(true);
|
|
|
- setCompleter(nullptr);
|
|
|
-
|
|
|
-
|
|
|
- connect(this, (void (QComboBox::*)(const QString &))&QComboBox::currentIndexChanged, this, &SearchComboBox::OnCurrentIndexChanged);
|
|
|
-}
|
|
|
-
|
|
|
-SearchComboBox::~SearchComboBox()
|
|
|
-{
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-void SearchComboBox::showPopup()
|
|
|
-{
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- QComboBox::showPopup();
|
|
|
-
|
|
|
- if (nullptr != view()) {
|
|
|
- view()->setMinimumWidth(width() + LISTVIEW_MARGIN * 2);
|
|
|
- }
|
|
|
- QWidget *popup = findChild<QFrame*>();
|
|
|
- if (nullptr != popup) {
|
|
|
- popup->move(mapToGlobal(QPoint(-LISTVIEW_MARGIN, height() - LISTVIEW_MARGIN + 4)));
|
|
|
- }
|
|
|
- m_showPopup = true;
|
|
|
-}
|
|
|
-
|
|
|
-void SearchComboBox::hidePopup()
|
|
|
-{
|
|
|
- QComboBox::hidePopup();
|
|
|
- m_showPopup = false;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-void SearchComboBox::setViewShadowEffect()
|
|
|
-{
|
|
|
- setView(new QListView());
|
|
|
- view()->window()->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);
|
|
|
- view()->window()->setAttribute(Qt::WA_TranslucentBackground);
|
|
|
-
|
|
|
-
|
|
|
- QApplication::setEffectEnabled(Qt::UI_AnimateCombo, false);
|
|
|
-
|
|
|
-
|
|
|
- QGraphicsDropShadowEffect *pShadowEffect = new QGraphicsDropShadowEffect(this);
|
|
|
- pShadowEffect->setBlurRadius(10);
|
|
|
- pShadowEffect->setColor(QColor(0, 0, 0, 60));
|
|
|
- pShadowEffect->setOffset(0, 0);
|
|
|
- view()->setGraphicsEffect(pShadowEffect);
|
|
|
-}
|
|
|
-
|
|
|
-void SearchComboBox::wheelEvent(QWheelEvent *e)
|
|
|
-{
|
|
|
- Q_UNUSED(e);
|
|
|
-
|
|
|
-}
|
|
|
-bool SearchComboBox::event(QEvent *event)
|
|
|
-{
|
|
|
-
|
|
|
- if(event->type()==QEvent::Wheel)
|
|
|
- {
|
|
|
- return true;
|
|
|
- }
|
|
|
- else if(event->type() == QEvent::KeyPress)
|
|
|
- {
|
|
|
- QKeyEvent* ev = (QKeyEvent*)event;
|
|
|
- if(ev->key() == Qt::Key_Enter || ev->key() == Qt::Key_Return){
|
|
|
- if(isEditable()){
|
|
|
- autoquery();
|
|
|
- m_autoquery = false;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- else if(event->type() == QEvent::MouseButtonPress)
|
|
|
- {
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
- else if(event->type() == QEvent::FocusOut)
|
|
|
- {
|
|
|
-
|
|
|
- if(!m_showPopup)
|
|
|
- {
|
|
|
- if(!m_currentText.isEmpty())
|
|
|
- {
|
|
|
- setCurrentText(m_currentText);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- int index = currentIndex();
|
|
|
- if(index >= 0 && index < m_items.count())
|
|
|
- {
|
|
|
- setCurrentText(m_items[index].text);
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
- update();
|
|
|
- }
|
|
|
- }
|
|
|
- return QComboBox::event(event);
|
|
|
-}
|
|
|
-
|
|
|
-void SearchComboBox::OnCurrentIndexChanged(const QString &text)
|
|
|
-{
|
|
|
- m_currentText = text;
|
|
|
-}
|
|
|
-
|
|
|
-void SearchComboBox::addItem(const QString &text, const QVariant &userData)
|
|
|
-{
|
|
|
- ItemData item;
|
|
|
- item.text = text;
|
|
|
- item.userdata = userData;
|
|
|
- item.alphbat = WordToPinyin::firstPinyin(item.text);
|
|
|
- m_items.append(item);
|
|
|
- QComboBox::addItem(item.text, item.userdata);
|
|
|
-}
|
|
|
-
|
|
|
-void SearchComboBox::clear()
|
|
|
-{
|
|
|
- m_items.clear();
|
|
|
- QComboBox::clear();
|
|
|
-}
|
|
|
-
|
|
|
-void SearchComboBox::autoquery()
|
|
|
-{
|
|
|
- QString text = currentText().toLower();
|
|
|
- QComboBox::clear();
|
|
|
- if(text.isEmpty())
|
|
|
- {
|
|
|
-
|
|
|
- foreach(ItemData item,m_items)
|
|
|
- {
|
|
|
- QComboBox::addItem(item.text, item.userdata);
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
-
|
|
|
- foreach(ItemData item, m_items)
|
|
|
- {
|
|
|
- if(item.alphbat.toLower().contains(text) || item.text.toLower().contains(text))
|
|
|
- {
|
|
|
- QComboBox::addItem(item.text, item.userdata);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
- showPopup();
|
|
|
-}
|