Skip to content

Commit

Permalink
fix chapters model update, add some padding to the manga details icon
Browse files Browse the repository at this point in the history
  • Loading branch information
mgn-norm committed Sep 20, 2021
1 parent 5843bea commit 2c46ccd
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/models/ChaptersModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ void ChaptersModel::classBegin()
{
}

/******************************************************************************
*
* gotChapters
*
*****************************************************************************/
void ChaptersModel::gotChapters(const QJsonDocument& reply)
{
if (reply.isEmpty()) {
Expand All @@ -53,7 +58,7 @@ void ChaptersModel::gotChapters(const QJsonDocument& reply)

_chapters.clear();

beginInsertRows({}, _chapters.size(), _chapters.size() + reply.array().count() - 1);
beginResetModel();

for (const auto& entry_arr : reply.array()) {
const auto& entry = entry_arr.toObject();
Expand All @@ -75,7 +80,7 @@ void ChaptersModel::gotChapters(const QJsonDocument& reply)
}
emit lastReadChapterChanged();

endInsertRows();
endResetModel();

if (!_cachedChapters) {
_cachedChapters = true;
Expand Down
10 changes: 10 additions & 0 deletions src/models/DownloadsModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ void DownloadsModel::componentComplete()
_webSocket.open(QUrl(resolved));
}

/******************************************************************************
*
* closed
*
*****************************************************************************/
void DownloadsModel::closed()
{
}
Expand All @@ -67,6 +72,11 @@ void DownloadsModel::onConnected()
this, &DownloadsModel::onTextMessageReceived);
}

/******************************************************************************
*
* onTextMessageReceived
*
*****************************************************************************/
void DownloadsModel::onTextMessageReceived(const QString& message)
{
QJsonDocument doc = QJsonDocument::fromJson(message.toUtf8());
Expand Down
11 changes: 11 additions & 0 deletions src/pinchzoomitem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "pinchzoomitem.h"

#include <QQmlEngine>
#include <qcoreapplication.h>

#define exportQmlType(ns, cls) qmlRegisterType<cls>(#ns, 1, 0, #cls)
#define IMPLEMENT_QTQUICK_TYPE(ns, cls) \
void register ## cls ## Type() { exportQmlType(ns, cls); } \
Q_COREAPP_STARTUP_FUNCTION(register ## cls ## Type)

IMPLEMENT_QTQUICK_TYPE(Tachidesk.Util, PinchZoomItem)
96 changes: 96 additions & 0 deletions src/pinchzoomitem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#pragma once

#include <QQuickItem>
#include <QLineF>
#include <QDebug>

/**
* @brief The PinchZoomItem class
* @author ARC
*/
class PinchZoomItem : public QQuickItem
{
Q_OBJECT
Q_PROPERTY(QQuickItem* zoomTarget MEMBER mTarget)
Q_PROPERTY(qreal wheelFactor MEMBER mWheelFactor)
Q_PROPERTY(qreal zoomFactor MEMBER mFactor)
Q_PROPERTY(qreal zoomMax MEMBER mMax)
Q_PROPERTY(qreal zoomMin MEMBER mMin)
public:
PinchZoomItem(QQuickItem *parent = 0) :
QQuickItem(parent),
mTarget(Q_NULLPTR),
mWheelFactor(0.1),
mCurrentFactor(1),
mFactor(1),
mMax(-1),
mMin(0.5)
{
connect(this, &QQuickItem::widthChanged, this, &PinchZoomItem::setDefaultMax);
connect(this, &QQuickItem::heightChanged, this, &PinchZoomItem::setDefaultMax);
}

signals:

public slots:

protected:
void touchEvent(QTouchEvent *touchEvent) Q_DECL_OVERRIDE {
switch (touchEvent->type()) {
case QTouchEvent::TouchBegin:
case QTouchEvent::TouchUpdate:
case QTouchEvent::TouchEnd: {
QList<QTouchEvent::TouchPoint> touchPoints = touchEvent->touchPoints();
if(touchPoints.count() == 2) {
const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
qreal s = QLineF(touchPoint0.pos(), touchPoint1.pos()).length() / \
QLineF(touchPoint0.startPos(), touchPoint1.startPos()).length();
if(touchEvent->touchPointStates() & Qt::TouchPointReleased) {
mCurrentFactor = zoom(mCurrentFactor * s);
} else {
zoom(mCurrentFactor * s);
}
}
}
default:
break;
}
}

void wheelEvent(QWheelEvent* wheelEvent) Q_DECL_OVERRIDE{
mCurrentFactor = zoom(mCurrentFactor + (wheelEvent->angleDelta().y() > 0? 1:-1) * mWheelFactor);
}

void componentComplete() Q_DECL_OVERRIDE {
QQuickItem::componentComplete();
if(mTarget == Q_NULLPTR && !childItems().isEmpty())
mTarget = childItems().first();
}
private slots:
void setDefaultMax(){
if(mMax == -1){
mMax = mTarget->parentItem()->width() / mTarget->width();
if(mMax < mMin)
qWarning()<<metaObject()->className()<<"Zoom min have to be smaller than max.";
}
}

private:
inline qreal zoom(const qreal& f){
if(mTarget != Q_NULLPTR && mMin < f && f < mMax){
mTarget->setScale(f * mFactor);
return f;
} else {
return mCurrentFactor;
}
}

QQuickItem* mTarget;
qreal mWheelFactor;
qreal mCurrentFactor;
qreal mFactor;
qreal mMax;
qreal mMin;
};

3 changes: 3 additions & 0 deletions src/qml/MangaDetails.qml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ Item {
Column {
id: detailsColumn
spacing: 8
leftPadding: 4
rightPadding: 4
topPadding: 4
width: parent.width
height: {
var heightLimit = parent.height * 3/8
Expand Down

0 comments on commit 2c46ccd

Please sign in to comment.