Add support for touch sliding.

This commit is contained in:
iaom 2021-09-27 17:25:06 +08:00
parent 5d29be6ebe
commit 73de53d384
6 changed files with 82 additions and 22 deletions

View File

@ -42,6 +42,7 @@ using namespace Zeeker;
ResultArea::ResultArea(QWidget *parent) : QScrollArea(parent)
{
qRegisterMetaType<SearchPluginIface::ResultInfo>("SearchPluginIface::ResultInfo");
this->viewport()->setAttribute(Qt::WA_AcceptTouchEvents);
initUi();
initConnections();
}
@ -347,6 +348,55 @@ void ResultArea::setSelectionInfo(QString &pluginID)
}
}
void ResultArea::mousePressEvent(QMouseEvent *event)
{
// qDebug() << "mouse pressed" << event->source() << event->button();
// m_pressPoint = event->pos();
return QScrollArea::mousePressEvent(event);
}
void ResultArea::mouseMoveEvent(QMouseEvent *event)
{
// if(m_pressPoint.isNull()) {
// return QScrollArea::mouseMoveEvent(event);
// }
// int delta = ((event->pos().y() - m_pressPoint.y()) / this->widget()->height()) * verticalScrollBar()->maximum();
// this->verticalScrollBar()->setValue(verticalScrollBar()->value() + delta);
// event->accept();
return QScrollArea::mouseMoveEvent(event);
}
void ResultArea::mouseReleaseEvent(QMouseEvent *event)
{
return QScrollArea::mouseReleaseEvent(event);
}
bool ResultArea::viewportEvent(QEvent *event)
{
if(event->type() == QEvent::TouchBegin) {
QTouchEvent *e = dynamic_cast<QTouchEvent *>(event);
if(e->touchPoints().size() == 1) {
m_pressPoint = m_widget->mapFrom(this, e->touchPoints().at(0).pos().toPoint());
event->accept();
return true;
}
} else if (event->type() == QEvent::TouchUpdate) {
QTouchEvent *e = dynamic_cast<QTouchEvent *>(event);
// qDebug() << "touchpoint===========" << e->touchPoints().size();
if(e->touchPoints().size() == 1) {
int delta = m_pressPoint.y() - m_widget->mapFrom(this, e->touchPoints().at(0).pos().toPoint()).y();
// qDebug() << "last pos:" << m_pressPoint.y();
// qDebug() << "new pos:" << m_widget->mapFrom(this, e->touchPoints().at(0).pos().toPoint()).y();
// qDebug() << "delta" << delta;
// qDebug() << "height" << m_widget->height() << "--" << verticalScrollBar()->maximum();
this->verticalScrollBar()->setValue(verticalScrollBar()->value() + delta);
m_pressPoint = m_widget->mapFrom(this,e->touchPoints().at(0).pos().toPoint());
return true;
}
}
return QScrollArea::viewportEvent(event);
}
void ResultArea::initUi()
{
// this->verticalScrollBar()->setProperty("drawScrollBarGroove", false);

View File

@ -50,6 +50,12 @@ public:
public Q_SLOTS:
void onWidgetSizeChanged();
void setSelectionInfo(QString &pluginID);
protected:
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
bool viewportEvent(QEvent *event);
private:
void initUi();
@ -65,6 +71,7 @@ private:
bool m_detail_open_state = false;
bool m_is_selected = false;
QString m_selectedPluginID;
QPoint m_pressPoint;
Q_SIGNALS:
void startSearch(const QString &);

View File

@ -80,6 +80,7 @@ void ReceiveResultThread::run()
is_empty = false;
if(!m_result_queue->isEmpty()) {
Q_EMIT this->gotResultInfo(m_result_queue->dequeue());
} else {
is_empty = true;
}

View File

@ -155,22 +155,22 @@ void BestListView::mousePressEvent(QMouseEvent *event)
void BestListView::mouseReleaseEvent(QMouseEvent *event)
{
QModelIndex index = indexAt(event->pos());
if (index.isValid()) {
Q_EMIT this->clicked(index);
} else {
Q_EMIT this->clicked(this->currentIndex());
}
// QModelIndex index = indexAt(event->pos());
// if (index.isValid()) {
// Q_EMIT this->clicked(index);
// } else {
// Q_EMIT this->clicked(this->currentIndex());
// }
return QTreeView::mouseReleaseEvent(event);
}
void BestListView::mouseMoveEvent(QMouseEvent *event)
{
m_tmpCurrentIndex = this->currentIndex();
m_tmpMousePressIndex = indexAt(event->pos());
if (m_tmpMousePressIndex.isValid() and m_tmpCurrentIndex != m_tmpMousePressIndex) {
Q_EMIT this->clicked(m_tmpMousePressIndex);
}
// m_tmpCurrentIndex = this->currentIndex();
// m_tmpMousePressIndex = indexAt(event->pos());
// if (m_tmpMousePressIndex.isValid() and m_tmpCurrentIndex != m_tmpMousePressIndex) {
// Q_EMIT this->clicked(m_tmpMousePressIndex);
// }
return QTreeView::mouseMoveEvent(event);
}

View File

@ -29,6 +29,7 @@ void ResultViewDelegate::paint(QPainter * painter, const QStyleOptionViewItem &
style->drawControl(QStyle::CE_ItemViewItem, &optionV4, painter); //绘制非文本区域内容
if(index.model()->data(index, Qt::DisplayRole).toString().isEmpty()) return;
//fix me: for files which name begin with some ' ' , space will be hide...
QTextDocument doc;
doc.setHtml(getHtmlText(painter, option, index)); //提取富文本
QAbstractTextDocumentLayout::PaintContext ctx;

View File

@ -283,6 +283,7 @@ void ResultView::onMenuTriggered(QAction *action)
void ResultView::mousePressEvent(QMouseEvent *event)
{
// qDebug() << "source" << event->source();
m_tmpCurrentIndex = this->currentIndex();
m_tmpMousePressIndex = indexAt(event->pos());
if (m_tmpMousePressIndex.isValid() and m_tmpCurrentIndex != m_tmpMousePressIndex) {
@ -294,22 +295,22 @@ void ResultView::mousePressEvent(QMouseEvent *event)
void ResultView::mouseReleaseEvent(QMouseEvent *event)
{
QModelIndex index = indexAt(event->pos());
if (index.isValid()) {
Q_EMIT this->clicked(index);
} else {
Q_EMIT this->clicked(this->currentIndex());
}
// QModelIndex index = indexAt(event->pos());
// if (index.isValid()) {
// Q_EMIT this->clicked(index);
// } else {
// Q_EMIT this->clicked(this->currentIndex());
// }
return QTreeView::mouseReleaseEvent(event);
}
void ResultView::mouseMoveEvent(QMouseEvent *event)
{
m_tmpCurrentIndex = this->currentIndex();
m_tmpMousePressIndex = indexAt(event->pos());
if (m_tmpMousePressIndex.isValid() and m_tmpCurrentIndex != m_tmpMousePressIndex) {
Q_EMIT this->clicked(m_tmpMousePressIndex);
}
// m_tmpCurrentIndex = this->currentIndex();
// m_tmpMousePressIndex = indexAt(event->pos());
// if (m_tmpMousePressIndex.isValid() and m_tmpCurrentIndex != m_tmpMousePressIndex and event->source() != Qt::MouseEventSynthesizedByQt) {
// Q_EMIT this->clicked(m_tmpMousePressIndex);
// }
return QTreeView::mouseMoveEvent(event);
}