From 5badab752aedd4a4d90c789135e23148977bce5c Mon Sep 17 00:00:00 2001 From: "P.L. Lucas" Date: Sat, 10 Aug 2019 19:02:22 +0200 Subject: [PATCH] Set mouse size has been added. Related to #350 #953 mouse cursor size is not adjustable. --- liblxqt-config-cursor/cfgfile.cpp | 5 +- liblxqt-config-cursor/cfgfile.h | 2 +- liblxqt-config-cursor/crtheme.cpp | 5 +- liblxqt-config-cursor/crtheme.h | 2 +- liblxqt-config-cursor/previewwidget.cpp | 39 +++++++++++-- liblxqt-config-cursor/previewwidget.h | 9 ++- liblxqt-config-cursor/selectwnd.cpp | 48 +++++++++++---- liblxqt-config-cursor/selectwnd.h | 1 + liblxqt-config-cursor/selectwnd.ui | 77 ++++++++++++++++--------- 9 files changed, 140 insertions(+), 48 deletions(-) diff --git a/liblxqt-config-cursor/cfgfile.cpp b/liblxqt-config-cursor/cfgfile.cpp index 9794c09fc..39f5e6067 100644 --- a/liblxqt-config-cursor/cfgfile.cpp +++ b/liblxqt-config-cursor/cfgfile.cpp @@ -54,7 +54,7 @@ QMultiMap loadCfgFile(const QString &fname, bool forceLoCase) return res; } -void fixXDefaults(const QString &themeName) +void fixXDefaults(const QString &themeName, int cursorSize) { QStringList lst; { @@ -68,7 +68,7 @@ void fixXDefaults(const QString &themeName) QString s = stream.readLine(); if (s.isNull()) break; - // if the line does not contain Xcursor?theme, save it to a list + // if the line does not contain Xcursor.theme, save it to a list if (!(s.startsWith(QLatin1String("Xcursor")) && s.midRef(8).startsWith(QLatin1String("theme")))) lst << s; } @@ -93,6 +93,7 @@ void fixXDefaults(const QString &themeName) stream << s << "\n"; } stream << "\nXcursor.theme: " << themeName << "\n"; + stream << "\nXcursor.size: " << cursorSize << "\n"; fl.close(); } } diff --git a/liblxqt-config-cursor/cfgfile.h b/liblxqt-config-cursor/cfgfile.h index a6d7528f0..bbca75c89 100644 --- a/liblxqt-config-cursor/cfgfile.h +++ b/liblxqt-config-cursor/cfgfile.h @@ -14,7 +14,7 @@ #include QMultiMap loadCfgFile(const QString &fname, bool forceLoCase=false); -void fixXDefaults(const QString &themeName); +void fixXDefaults(const QString &themeName, int cursorSize); const QString findDefaultTheme(); #endif diff --git a/liblxqt-config-cursor/crtheme.cpp b/liblxqt-config-cursor/crtheme.cpp index d17cd6e58..dfbdf0e8f 100644 --- a/liblxqt-config-cursor/crtheme.cpp +++ b/liblxqt-config-cursor/crtheme.cpp @@ -227,12 +227,15 @@ bool haveXfixes() return result; } -bool applyTheme(const XCursorThemeData &theme) +bool applyTheme(const XCursorThemeData &theme, int cursorSize) { // Require the Xcursor version that shipped with X11R6.9 or greater, since // in previous versions the Xfixes code wasn't enabled due to a bug in the // build system (freedesktop bug #975). if (!haveXfixes()) return false; + + // Sets default cursor size + XcursorSetDefaultSize(QX11Info::display(), cursorSize); QByteArray themeName = QFile::encodeName(theme.name()); diff --git a/liblxqt-config-cursor/crtheme.h b/liblxqt-config-cursor/crtheme.h index 83cd5414c..e6909a4ae 100644 --- a/liblxqt-config-cursor/crtheme.h +++ b/liblxqt-config-cursor/crtheme.h @@ -112,7 +112,7 @@ class XCursorThemeData }; bool haveXfixes(); -bool applyTheme(const XCursorThemeData &theme); +bool applyTheme(const XCursorThemeData &theme, int cursorSize); QString getCurrentTheme(); diff --git a/liblxqt-config-cursor/previewwidget.cpp b/liblxqt-config-cursor/previewwidget.cpp index 30cd5194e..812b59f53 100644 --- a/liblxqt-config-cursor/previewwidget.cpp +++ b/liblxqt-config-cursor/previewwidget.cpp @@ -76,11 +76,13 @@ class PreviewCursor QPoint position () const { return mPos; } operator const xcb_cursor_t& () const { return mCursorHandle; } operator const QPixmap& () const { return pixmap(); } + const QString &getName() const { return mName; } private: QPixmap mPixmap; xcb_cursor_t mCursorHandle; QPoint mPos; + QString mName; }; @@ -96,6 +98,7 @@ PreviewCursor::PreviewCursor(const XCursorThemeData &theme, const QString &name) mPixmap = QPixmap::fromImage(image); // load the cursor mCursorHandle = theme.loadCursorHandle(name, previewSize); + mName = name; } QRect PreviewCursor::rect() const @@ -109,6 +112,8 @@ PreviewWidget::PreviewWidget(QWidget *parent) : QWidget(parent) { setMouseTracking(true); mCurrent = NULL; + mCursorSize = 16; // It usually is the default cursor size + mCurrentCursorSize = 16; } PreviewWidget::~PreviewWidget() @@ -120,6 +125,7 @@ PreviewWidget::~PreviewWidget() // Since Qt5, wrapping a Cursor handle with QCursor is no longer supported. // So we have to do it ourselves. I really hate Qt 5! +// Update: Qt 5.12 setCursor works properly void PreviewWidget::setCursorHandle(xcb_cursor_t cursorHandle) { WId wid = nativeParentWidget()->windowHandle()->winId(); @@ -157,11 +163,12 @@ void PreviewWidget::layoutItems() mNeedLayout = false; } -void PreviewWidget::setTheme(const XCursorThemeData &theme) +void PreviewWidget::setTheme(const XCursorThemeData *theme) { + mTheme = theme; qDeleteAll(mList); mList.clear(); - for (int i = 0; i < numCursors; ++i) mList << new PreviewCursor(theme, QString::fromUtf8(cursorNames[i])); + for (int i = 0; i < numCursors; ++i) mList << new PreviewCursor(*theme, QString::fromUtf8(cursorNames[i])); mNeedLayout = true; updateGeometry(); mCurrent = NULL; @@ -174,6 +181,7 @@ void PreviewWidget::clearTheme() qDeleteAll(mList); mList.clear(); mCurrent = NULL; + mTheme = nullptr; update(); } @@ -204,7 +212,11 @@ void PreviewWidget::mouseMoveEvent(QMouseEvent *e) // cheat Qt so it knows that the current cursor is not Qt::ArrowCursor. // This is a dirty hack, but without this, Qt cannot restore Qt::ArrowCursor later. setCursor(Qt::BlankCursor); - setCursorHandle(*c); + //setCursorHandle(*c); // Use default Qt5 setCursor: + if(mTheme != nullptr) { + QImage image = mTheme->loadImage(c->getName(), mCursorSize); + if (! image.isNull()) setCursor(QPixmap::fromImage(image)); + } mCurrent = c; } return; @@ -214,8 +226,27 @@ void PreviewWidget::mouseMoveEvent(QMouseEvent *e) mCurrent = NULL; } - void PreviewWidget::resizeEvent(QResizeEvent *) { if (!mList.isEmpty()) mNeedLayout = true; } + +void PreviewWidget::setCursorSize(int size) +{ + mCursorSize = size; +} + +int PreviewWidget::getCursorSize() +{ + return mCursorSize; +} + +void PreviewWidget::setCurrentCursorSize(int size) +{ + mCurrentCursorSize = size; +} + +int PreviewWidget::getCurrentCursorSize() +{ + return mCurrentCursorSize; +} \ No newline at end of file diff --git a/liblxqt-config-cursor/previewwidget.h b/liblxqt-config-cursor/previewwidget.h index 3d035ea81..491ab9f71 100644 --- a/liblxqt-config-cursor/previewwidget.h +++ b/liblxqt-config-cursor/previewwidget.h @@ -37,12 +37,16 @@ class PreviewWidget : public QWidget PreviewWidget (QWidget *parent=0); ~PreviewWidget (); - void setTheme (const XCursorThemeData &theme); + void setTheme (const XCursorThemeData *theme); void clearTheme (); QSize sizeHint () const; void setCursorHandle(xcb_cursor_t cursorHandle); + void setCursorSize(int size); + int getCursorSize(); + void setCurrentCursorSize(int size); + int getCurrentCursorSize(); protected: void paintEvent (QPaintEvent *e); @@ -55,6 +59,9 @@ class PreviewWidget : public QWidget QList mList; const PreviewCursor *mCurrent; bool mNeedLayout; + int mCursorSize; + int mCurrentCursorSize; + const XCursorThemeData *mTheme; }; #endif diff --git a/liblxqt-config-cursor/selectwnd.cpp b/liblxqt-config-cursor/selectwnd.cpp index b70f3e186..14caa84dd 100644 --- a/liblxqt-config-cursor/selectwnd.cpp +++ b/liblxqt-config-cursor/selectwnd.cpp @@ -36,6 +36,9 @@ #include #include +#include +#include + const QString HOME_ICON_DIR(QDir::homePath() + QStringLiteral("/.icons")); SelectWnd::SelectWnd(LXQt::Settings* settings, QWidget *parent) @@ -45,6 +48,8 @@ SelectWnd::SelectWnd(LXQt::Settings* settings, QWidget *parent) { ui->setupUi(this); ui->warningLabel->hide(); + ui->preview->setCurrentCursorSize(XcursorGetDefaultSize(QX11Info::display())); + ui->preview->setCursorSize(ui->preview->getCurrentCursorSize()); mModel = new XCursorThemeModel(this); @@ -66,6 +71,12 @@ SelectWnd::SelectWnd(LXQt::Settings* settings, QWidget *parent) connect(ui->warningLabel, SIGNAL(showDirInfo()), this, SLOT(showDirInfo())); + + // Set actual cursor size + ui->cursorSizeSpinBox->setValue(XcursorGetDefaultSize(QX11Info::display())); + + connect(ui->cursorSizeSpinBox, SIGNAL( valueChanged(int)), + this, SLOT(cursorSizeChaged(int))); // Disable the install button if we can't install new themes to ~/.icons, // or Xcursor isn't set up to look for cursor themes there @@ -102,7 +113,7 @@ void SelectWnd::setCurrent() selectRow(mAppliedIndex); ui->lbThemes->scrollTo(mAppliedIndex, QListView::PositionAtCenter); // Update the preview widget as well - if (theme) ui->preview->setTheme(*theme);// else ui->preview->clearTheme(); + if (theme) ui->preview->setTheme(theme);// else ui->preview->clearTheme(); } } @@ -136,7 +147,7 @@ void SelectWnd::currentChanged(const QModelIndex ¤t, const QModelIndex &pr if (current.isValid()) { const XCursorThemeData *theme = mModel->theme(current); if (theme) { - ui->preview->setTheme(*theme); + ui->preview->setTheme(theme); ui->btRemove->setEnabled(theme->isWritable()); } else { ui->preview->clearTheme(); @@ -159,11 +170,18 @@ void SelectWnd::applyCusorTheme() QModelIndex curIndex = ui->lbThemes->currentIndex(); if(!curIndex.isValid()) return; const XCursorThemeData *theme = mModel->theme(curIndex); - if(!theme || mSettings->value(QStringLiteral("Mouse/cursor_theme")) == theme->name()) { + + if(!theme || + ( + mSettings->value(QStringLiteral("Mouse/cursor_theme")) == theme->name() + && mSettings->value(QStringLiteral("Mouse/cursor_size")) == ui->cursorSizeSpinBox->value() + ) + ) { return; } - applyTheme(*theme); - fixXDefaults(theme->name()); + + applyTheme(*theme, ui->cursorSizeSpinBox->value()); + fixXDefaults(theme->name(), ui->cursorSizeSpinBox->value()); // call xrdb to merge the new settings in ~/.Xdefaults // FIXME: need to check if we're running in X? @@ -179,6 +197,7 @@ void SelectWnd::applyCusorTheme() // save to Mouse/cursor_theme instead mSettings->beginGroup(QStringLiteral("Mouse")); mSettings->setValue(QStringLiteral("cursor_theme"), theme->name()); + mSettings->setValue(QStringLiteral("cursor_size"), ui->cursorSizeSpinBox->value()); mSettings->endGroup(); // The XCURSOR_THEME environment variable does not work sometimes. @@ -197,7 +216,8 @@ void SelectWnd::applyCusorTheme() "[Icon Theme]\n" << "Name=Default\n" << "Comment=Default cursor theme\n" << - "Inherits=" << theme->name() << "\n"; + "Inherits=" << theme->name() << "\n" << + "Size=" << ui->cursorSizeSpinBox->value() << "\n"; indexTheme.close(); } } @@ -222,13 +242,19 @@ void SelectWnd::on_btRemove_clicked() void SelectWnd::handleWarning() { - bool empty = mModel->rowCount(); - ui->warningLabel->setVisible(!empty); - ui->preview->setVisible(empty); - ui->infoLabel->setVisible(empty); + bool empty = mModel->rowCount(); + ui->warningLabel->setVisible(!empty); + ui->preview->setVisible(empty); + ui->infoLabel->setVisible(empty); } void SelectWnd::showDirInfo() { - QToolTip::showText(mapToGlobal(ui->warningLabel->buttonPos()), mModel->searchPaths().join(QStringLiteral("\n"))); + QToolTip::showText(mapToGlobal(ui->warningLabel->buttonPos()), mModel->searchPaths().join(QStringLiteral("\n"))); +} + +void SelectWnd::cursorSizeChaged(int size) +{ + ui->preview->setCursorSize(size); + emit settingsChanged(); } diff --git a/liblxqt-config-cursor/selectwnd.h b/liblxqt-config-cursor/selectwnd.h index 0c080c0bb..82fa6d966 100644 --- a/liblxqt-config-cursor/selectwnd.h +++ b/liblxqt-config-cursor/selectwnd.h @@ -58,6 +58,7 @@ private slots: void on_btRemove_clicked (); void handleWarning(); void showDirInfo(); + void cursorSizeChaged(int size); private: XCursorThemeModel *mModel; diff --git a/liblxqt-config-cursor/selectwnd.ui b/liblxqt-config-cursor/selectwnd.ui index f6cd6b91f..2c8a48538 100644 --- a/liblxqt-config-cursor/selectwnd.ui +++ b/liblxqt-config-cursor/selectwnd.ui @@ -6,28 +6,25 @@ 0 0 - 384 - 353 + 438 + 364 LXQt Mouse Theme Configuration - - - - Select the cursor theme you want to use (hover preview to test cursor). <b>LXQt session needs restart after this change</b>: + + + + false - - true + + &Install New Theme... - - - - + @@ -43,7 +40,17 @@ - + + + + Select the cursor theme you want to use (hover preview to test cursor). <b>LXQt session needs restart after this change</b>: + + + true + + + + QAbstractItemView::NoEditTriggers @@ -59,7 +66,33 @@ - + + + + LXQt session needs restart to view this change. + + + 12 + + + 128 + + + 16 + + + + + + + + + + &Remove Theme + + + + Qt::Horizontal @@ -72,20 +105,10 @@ - - - - false - + + - &Install New Theme... - - - - - - - &Remove Theme + Size