Skip to content

Commit

Permalink
Set mouse size has been added. Related to #350 #953 mouse cursor size…
Browse files Browse the repository at this point in the history
… is not adjustable.
  • Loading branch information
selairi authored and agaida committed Aug 17, 2019
1 parent 9117ad3 commit 5badab7
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 48 deletions.
5 changes: 3 additions & 2 deletions liblxqt-config-cursor/cfgfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ QMultiMap<QString, QString> loadCfgFile(const QString &fname, bool forceLoCase)
return res;
}

void fixXDefaults(const QString &themeName)
void fixXDefaults(const QString &themeName, int cursorSize)
{
QStringList lst;
{
Expand All @@ -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;
}
Expand All @@ -93,6 +93,7 @@ void fixXDefaults(const QString &themeName)
stream << s << "\n";
}
stream << "\nXcursor.theme: " << themeName << "\n";
stream << "\nXcursor.size: " << cursorSize << "\n";
fl.close();
}
}
Expand Down
2 changes: 1 addition & 1 deletion liblxqt-config-cursor/cfgfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <QString>

QMultiMap<QString, QString> loadCfgFile(const QString &fname, bool forceLoCase=false);
void fixXDefaults(const QString &themeName);
void fixXDefaults(const QString &themeName, int cursorSize);
const QString findDefaultTheme();

#endif
5 changes: 4 additions & 1 deletion liblxqt-config-cursor/crtheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
2 changes: 1 addition & 1 deletion liblxqt-config-cursor/crtheme.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class XCursorThemeData
};

bool haveXfixes();
bool applyTheme(const XCursorThemeData &theme);
bool applyTheme(const XCursorThemeData &theme, int cursorSize);

QString getCurrentTheme();

Expand Down
39 changes: 35 additions & 4 deletions liblxqt-config-cursor/previewwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};


Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -174,6 +181,7 @@ void PreviewWidget::clearTheme()
qDeleteAll(mList);
mList.clear();
mCurrent = NULL;
mTheme = nullptr;
update();
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
9 changes: 8 additions & 1 deletion liblxqt-config-cursor/previewwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -55,6 +59,9 @@ class PreviewWidget : public QWidget
QList<PreviewCursor *> mList;
const PreviewCursor *mCurrent;
bool mNeedLayout;
int mCursorSize;
int mCurrentCursorSize;
const XCursorThemeData *mTheme;
};

#endif
48 changes: 37 additions & 11 deletions liblxqt-config-cursor/selectwnd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
#include <QTextStream>
#include <QProcess>

#include <QX11Info>
#include <X11/Xcursor/Xcursor.h>

const QString HOME_ICON_DIR(QDir::homePath() + QStringLiteral("/.icons"));

SelectWnd::SelectWnd(LXQt::Settings* settings, QWidget *parent)
Expand All @@ -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);

Expand All @@ -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
Expand Down Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -136,7 +147,7 @@ void SelectWnd::currentChanged(const QModelIndex &current, 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();
Expand All @@ -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?
Expand All @@ -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.
Expand All @@ -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();
}
}
Expand All @@ -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();
}
1 change: 1 addition & 0 deletions liblxqt-config-cursor/selectwnd.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ private slots:
void on_btRemove_clicked ();
void handleWarning();
void showDirInfo();
void cursorSizeChaged(int size);

private:
XCursorThemeModel *mModel;
Expand Down
Loading

0 comments on commit 5badab7

Please sign in to comment.