#include "stdafx.h"
#include <QDir>
#include <QSettings>
#include <QTextCodec>
#include <QScrollBar>
#include "GMPSearchEdit.h"
#include "ui_GMPSearchEdit.h"
static const char* con_Search = QT_TRANSLATE_NOOP("GMPSearchEdit", "搜索");
GMPSearchEdit::GMPSearchEdit( QWidget *parent )
: QFrame(parent),
ui(new Ui::GMPSearchEditWidget),
m_strLastInput("")
{
Q_ASSERT(parent);
ui->setupUi(this);
ui->btnClear->hide();
connect(ui->leText, SIGNAL(textChanged(const QString &)), this, SLOT(slotOnTextEdited(const QString &)));
connect(ui->leText, SIGNAL(signalFocusIn()), this, SLOT(slotOnLEFocusIn()));
connect(ui->leText, SIGNAL(signalFocusOut()), this, SLOT(slotOnLEFocusOut()));
connect(ui->leText, &GMPLineEditForSearchEdit::signalReturnPressed, this, [this]{
if (m_pInputHistory->isVisible())
{
QString selectText = m_pInputHistory->currentText();
if (!selectText.isEmpty())
{
ui->leText->setText(selectText);
}
}
m_pInputHistory->hide();
slotOnReturnPressed();
});
connect(ui->leText, SIGNAL(signalEscPressed()), this, SIGNAL(signalEscPressed()));
connect(ui->btnClear, &QPushButton::clicked, ui->leText, [this]{
QString searchText = ui->leText->text();
if (searchText.isEmpty() == false)
{
m_pInputHistory->refresh();
}
ui->leText->clear();
});
m_pInputHistory = new GMPSearchHistoryList(this);
connect(ui->leText, SIGNAL(signalMoveCurrentRow(bool)), m_pInputHistory, SLOT(moveCurrentRow(bool)));
m_pInputHistory->hide();
connect(m_pInputHistory, SIGNAL(signalSelectText(const QString &)), ui->leText, SLOT(setText(const QString &)));
}
GMPSearchEdit::~GMPSearchEdit()
{
delete ui;
}
void GMPSearchEdit::slotOnTextEdited( const QString &strText )
{
QString strTrimmedText = strText.trimmed();
bool bEmpty = strTrimmedText.isEmpty();
ui->btnClear->setVisible(!bEmpty);
ui->btnSearch->setVisible(bEmpty);
doSearch(strTrimmedText);
}
void GMPSearchEdit::slotOnLEFocusIn()
{
if (m_pInputHistory->count() > 0)
{
QPoint pos = rect().bottomLeft();
pos = mapToGlobal(pos);
m_pInputHistory->show();
m_pInputHistory->move(pos);
}
ui->leText->setPlaceholderText("");
}
void GMPSearchEdit::slotOnLEFocusOut()
{
m_pInputHistory->hide();
ui->leText->setPlaceholderText(m_strPlaceholderText);
}
void GMPSearchEdit::slotOnReturnPressed()
{
m_pInputHistory->hide();
QString strText = ui->leText->text().trimmed();
if (!strText.isEmpty())
{
m_pInputHistory->refresh();
}
}
bool GMPSearchEdit::eventFilter( QObject *obj, QEvent *ev )
{
if (ev->type() == QEvent::MouseButtonPress)
{
m_pInputHistory->hide();
return false;
}
return QFrame::eventFilter(obj, ev);
}
void GMPSearchEdit::doOnDirChanged( const QString &strDir )
{
QString strDirName;
QDir dir(strDir);
if (dir.exists()) //本地目录
{
strDirName = dir.dirName();
if (strDirName.isEmpty())
{
strDirName = strDir;
}
}
else //虚拟目录
{
int nIndex = strDir.lastIndexOf("\\");
if (nIndex != -1)
{
strDirName = strDir.mid(nIndex + 1);
}
else //Root
{
strDirName = strDir;
}
}
//renl-a 2017年11月29日 搜索云文件时 不显示双冒号
if (strDirName.startsWith("::"))
{
strDirName.remove("::");
}
m_strPlaceholderText = qApp->translate("GMPSearchEdit", con_Search) + " " + strDirName;
ui->leText->clear();
ui->leText->setPlaceholderText(m_strPlaceholderText);
}
void GMPSearchEdit::doSearch( const QString &strText )
{
if (strText != m_strLastInput)
{
emit signalSearch(strText);
m_strLastInput = strText;
}
}
void GMPSearchEdit::clear()
{
ui->leText->clear();
}
void GMPSearchEdit::doOnMove()
{
m_pInputHistory->hide();
}
void GMPSearchEdit::setLineEditFocus()
{
ui->leText->setFocus();
}
void GMPLineEditForSearchEdit::focusOutEvent( QFocusEvent * e )
{
QLineEdit::focusOutEvent(e);
emit signalFocusOut();
}
GMPLineEditForSearchEdit::GMPLineEditForSearchEdit( QWidget *parent )
: QLineEdit(parent)
{
}
void GMPLineEditForSearchEdit::mousePressEvent( QMouseEvent * e )
{
QLineEdit::mousePressEvent(e);
emit signalFocusIn();
}
void GMPLineEditForSearchEdit::keyPressEvent( QKeyEvent * event )
{
if (hasFocus())
{
if (event->key() == Qt::Key_Escape)
{
if (!text().isEmpty())
{
clear();
}
else
{
emit signalEscPressed();
}
return;
}
else if ((event->key() == Qt::Key_Return) || (event->key() == Qt::Key_Enter))
{
emit signalReturnPressed();
return;
}
else if (event->key() == Qt::Key_Down)
{
emit signalMoveCurrentRow(false);
}
else if (event->key() == Qt::Key_Up)
{
emit signalMoveCurrentRow(true);
}
}
QLineEdit::keyPressEvent(event);
}
GMPSearchList::GMPSearchList( QWidget * parent /*= nullptr*/ )
: QListWidget(parent)
{
setMouseTracking(true);
installEventFilter(this);
}
void GMPSearchList::mouseMoveEvent( QMouseEvent * mouseEvent )
{
QModelIndex index = this->indexAt(mouseEvent->pos());
if (index.isValid())
{
blockSignals(true);
this->setCurrentIndex(index);
blockSignals(false);
}
return __super::mouseMoveEvent(mouseEvent);
}
void GMPSearchList::leaveEvent( QEvent * e )
{
blockSignals(true);
setCurrentItem(nullptr);
setCurrentRow(-1);
blockSignals(false);
return __super::leaveEvent(e);
}
GMPSearchHistoryList::GMPSearchHistoryList( QWidget *parent )
: QFrame(parent)
{
Qt::WindowFlags flags = windowFlags();
flags = flags | Qt::Tool | Qt::FramelessWindowHint;
setWindowFlags(flags);
m_pHistoryList = new GMPSearchList(this);
m_pHistoryList->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
m_pHistoryList->setStyleSheet("QListWidget {border: no;}");
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
QVBoxLayout *pLayout = new QVBoxLayout(this);
pLayout->setContentsMargins(0, 0, 0, 0);
pLayout->addWidget(m_pHistoryList);
setLayout(pLayout);
initializeHistory();
setFixedWidth(158);
setFixedHeight(m_pHistoryList->count() * 20 + 4 + m_pHistoryList->count() * 2);
m_pHistoryList->setStyleSheet(QString("QListView::item{height:20px;}"
"QListView::item:hover{background:transparent;}"
"QListView::item:selected{background:#B3DBF6;}"));
m_pHistoryList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_pHistoryList->setSpacing(1);
connect(m_pHistoryList, SIGNAL(currentTextChanged(const QString &)), this, SIGNAL(signalSelectText(const QString &)));
}
void GMPSearchHistoryList::initializeHistory()
{
m_pHistoryList->clear();
QMap<QString, QString> searchItems;
searchItems["1"] = "1";
searchItems["1"] = "1";
searchItems["1"] = "1";
for (int i = searchItems.count() - 1; i >= 0; --i)
{
QString strValue = searchItems[QString::number(i)];
QListWidgetItem * pItem = new QListWidgetItem(strValue, m_pHistoryList);
m_pHistoryList->addItem(pItem);
}
setFixedHeight(m_pHistoryList->count() * 20 + 4 + m_pHistoryList->count() * 2);
}
int GMPSearchHistoryList::count()
{
return m_pHistoryList->count