Skip to content

Commit

Permalink
Simplified layout calculation of Kvantum Manger's conf page
Browse files Browse the repository at this point in the history
  • Loading branch information
tsujan committed Nov 19, 2022
1 parent efb3b39 commit a5ec320
Show file tree
Hide file tree
Showing 42 changed files with 2,274 additions and 2,345 deletions.
2 changes: 1 addition & 1 deletion Kvantum/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
V1.0.7
---------
* Don't rely on the (undocumented) "Destroy" event to remove destroyed widgets from lists. In this way, the cause of a rare crash is removed.
* Added a workaround to Kvantum Manager for a window resizing regression in a recent version of Qt 5.15. Also, improved the code for avoiding scrollbars in the 3rd page.
* Better guarantee for the lack of scrollbars in the config page of Kvantum Manager when it's visited for the first time.

V1.0.6
---------
Expand Down
2 changes: 1 addition & 1 deletion Kvantum/NEWS
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Latest version:

19 Nov 2022, V1.0.7
20 Nov 2022, V1.0.7

See "ChangeLog" for changes.
215 changes: 74 additions & 141 deletions Kvantum/kvantummanager/KvantumManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,7 @@ KvantumManager::KvantumManager (const QString& lang, QWidget *parent) : QMainWin
setWindowIcon (icn);
ui->preview->setIcon (icn);

/* The conf page is the largest and we want to avoid scrollbars in it
the first time it is shown. So, we force a layout calculation for it,
knowing that it is the current page in the ui file. But, first, the
text of "configLabel" should be set. */
setConfigLabel();
setAttribute (Qt::WA_DontShowOnScreen);
resize (minimumSizeHint().expandedTo (QSize (600, 400)));
show();
}
/*************************/
Expand All @@ -277,86 +272,6 @@ KvantumManager::~KvantumManager()
delete effect_;
}
/*************************/
void KvantumManager::showWindow()
{ // set the first page as the current page and really show the window
ui->toolBox->setCurrentIndex (0);
resize (minimumSizeHint().expandedTo (QSize (600, 400)));
hide();
setAttribute (Qt::WA_DontShowOnScreen, false);
QTimer::singleShot (0, this, [this] {
show();
raise();
activateWindow();
});
}
/*************************/
void KvantumManager::fitThirdPageToContents()
{
/* Avoid scrollbars in the conf page.
NOTE: The layout of the conf page should be completely
calculated when this function is called. */
if (auto viewport = ui->toolBox->widget (2)->parentWidget())
{
if (auto scrollArea = qobject_cast<QAbstractScrollArea*>(viewport->parentWidget()))
{
/* the maximum size inside the available geometry */
QSize maxSize;
QRect sr;
if (QWindow *win = windowHandle())
{
if (QScreen *sc = win->screen())
sr = sc->availableGeometry();
}
if (sr.isNull())
{
if (QScreen *pScreen = QApplication::primaryScreen())
sr = pScreen->availableGeometry();
}
if (!sr.isNull())
{
maxSize = sr.size()
// the window frame size
- (frameGeometry().size() - size());
}

#if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
/* WARNING: For some reason unknown to me, after a Qt5 upgrade,
the window will be resized correctly only if its
width is calculated and set after its height. */
int diff = viewport->childrenRect().height() - scrollArea->height();
if (diff > 0)
{
int newHeight = height() + diff;
if (maxSize.isValid())
newHeight = qMin (newHeight, maxSize.height());
resize (size().width(), newHeight);
}
diff = viewport->childrenRect().width() - scrollArea->width();
if (diff > 0)
{
int newWidth = width() + diff;
if (maxSize.isValid())
newWidth = qMin (newWidth, maxSize.width());
resize (newWidth, size().height());
}
#else
QSize diff = viewport->childrenRect().size() - scrollArea->size();
if (diff.width() > 0 || diff.height() > 0)
{
QSize newSize = size().expandedTo (size() + diff);
if (!sr.isNull())
{
newSize = newSize.boundedTo (sr.size()
// the window frame size
- (frameGeometry().size() - size()));
}
resize (newSize);
}
#endif
}
}
}
/*************************/
void KvantumManager::closeEvent (QCloseEvent *event)
{
process_->terminate();
Expand Down Expand Up @@ -1017,15 +932,11 @@ void KvantumManager::showAnimated (QWidget *w, int duration)
animation_->setEndValue (1.0);
animation_->start();

/* Qt has a scroll bug that shows up when SH_UnderlineShortcut
is set to "false" and interferes with horizontal scrolling
of the label in the third page. This is a workaround. */
if (ui->configLabel->isVisible())
{
connect (animation_, &QAbstractAnimation::finished,
ui->configLabel, QOverload<>::of(&QWidget::update),
Qt::UniqueConnection);
}
/* Qt has a scroll bug that shows up with SH_UnderlineShortcut set to
"false" and interferes with label scrolling. This is a workaround. */
connect (animation_, &QAbstractAnimation::finished,
w, QOverload<>::of(&QWidget::update),
Qt::UniqueConnection);
}
/*************************/
// Activates the theme and sets kvconfigTheme_.
Expand Down Expand Up @@ -1060,8 +971,7 @@ void KvantumManager::useTheme()

ui->useTheme->setEnabled (false);

/* this is needed if the config file is created by this method */
QCoreApplication::processEvents();
QCoreApplication::processEvents(); // needed if the config file is created by this method
restyleWindow();
if (process_->state() == QProcess::Running)
preview();
Expand Down Expand Up @@ -1346,6 +1256,42 @@ void KvantumManager::defaultThemeButtons()
respectDE (ui->checkBoxDE->isChecked());
}
/*************************/
void KvantumManager::fitConfPageToContents()
{
/* Avoid scrollbars in the conf page.
NOTE: The layout of the conf page should be completely
calculated when this function is called. */
if (auto viewport = ui->toolBox->widget (2)->parentWidget())
{
if (auto scrollArea = qobject_cast<QAbstractScrollArea*>(viewport->parentWidget()))
{
QSize diff = viewport->childrenRect().size() - scrollArea->size();
if (diff.width() > 0 || diff.height() > 0)
{
QSize newSize = size().expandedTo (size() + diff);
QRect sr;
if (QWindow *win = windowHandle())
{
if (QScreen *sc = win->screen())
sr = sc->availableGeometry();
}
if (sr.isNull())
{
if (QScreen *pScreen = QApplication::primaryScreen())
sr = pScreen->availableGeometry();
}
if (!sr.isNull())
{
newSize = newSize.boundedTo (sr.size()
// the window frame size
- (frameGeometry().size() - size()));
}
resize (newSize);
}
}
}
}
/*************************/
void KvantumManager::restyleWindow()
{
const QWidgetList topLevels = QApplication::topLevelWidgets();
Expand Down Expand Up @@ -1396,47 +1342,10 @@ void KvantumManager::restyleWindow()
}
/* avoid scrollbars in the conf page */
if (ui->toolBox->currentIndex() == 2)
fitThirdPageToContents();
fitConfPageToContents();
});
}
/*************************/
bool KvantumManager::setConfigLabel (bool animate)
{
bool hasUserSVG = false;
if (kvconfigTheme_.isEmpty())
{
ui->configLabel->setText (tr ("These are the settings that can be safely changed.<br>For the others, click <i>Save</i> and then edit this file:")
+ "<br><i>~/.config/Kvantum/Default#/<b>Default#.kvconfig</b></i>");
if (animate)
showAnimated (ui->configLabel, 1000);
ui->checkBoxPattern->setEnabled (false);
}
else
{
/* a config other than the default Kvantum one */
QString themeDir = userThemeDir (kvconfigTheme_);
userConfigFile_ = QString ("%1/%2.kvconfig").arg (themeDir).arg (kvconfigTheme_);
QString userSvg = QString ("%1/%2.svg").arg (themeDir).arg (kvconfigTheme_);
hasUserSVG = QFile::exists (userSvg);
if (!QFile::exists (userConfigFile_) && !hasUserSVG)
{ // no user theme but a root one
ui->configLabel->setText (tr ("These are the settings that can be safely changed.<br>For the others, click <i>Save</i> and then edit this file:")
+ QString ("<br><i>~/.config/Kvantum/%1#/<b>%1#.kvconfig</b></i>").arg (kvconfigTheme_));
}
else
{
/* If the user config file doesn't exist but the user SVG file does,
the default config file will be coppied to the user folder in
tabChanged() and this text will be correct. */
ui->configLabel->setText (tr ("These are the settings that can be safely changed.<br>For the others, edit this file:")
+ QString ("<a href='%2'><br><i>%1").arg (themeDir).arg (userConfigFile_) + QString ("/<b>%1.kvconfig</b></i></a>").arg (kvconfigTheme_));
}
if (animate)
showAnimated (ui->configLabel, 1000);
}
return hasUserSVG;
}
/*************************/
void KvantumManager::tabChanged (int index)
{
ui->statusBar->clearMessage();
Expand Down Expand Up @@ -1481,11 +1390,32 @@ void KvantumManager::tabChanged (int index)
defaultThemeButtons();
setTabWidgetFocus();

bool hasUserSVG = setConfigLabel (true); // also sets userConfigFile_

if (!kvconfigTheme_.isEmpty())
if (kvconfigTheme_.isEmpty())
{
ui->configLabel->setText (tr ("These are the settings that can be safely changed.<br>For the others, click <i>Save</i> and then edit this file:")
+ "<br><i>~/.config/Kvantum/Default#/<b>Default#.kvconfig</b></i>");
showAnimated (ui->configLabel, 1000);
ui->checkBoxPattern->setEnabled (false);
}
else
{
QString themeConfig = userConfigFile_; // might change below
/* a config other than the default Kvantum one */
QString themeDir = userThemeDir (kvconfigTheme_);
QString themeConfig = QString ("%1/%2.kvconfig").arg (themeDir).arg (kvconfigTheme_);
userConfigFile_ = themeConfig;
QString userSvg = QString ("%1/%2.svg").arg (themeDir).arg (kvconfigTheme_);

/* If themeConfig doesn't exist but userSvg does, themeConfig be created by copying
the default config below and this message will be correct. If neither themeConfig
nor userSvg exists, this message will be replaced by the one that follows it. */
ui->configLabel->setText (tr ("These are the settings that can be safely changed.<br>For the others, edit this file:")
+ QString ("<a href='%2'><br><i>%1").arg (themeDir).arg (userConfigFile_) + QString ("/<b>%1.kvconfig</b></i></a>").arg (kvconfigTheme_));
if (!QFile::exists (themeConfig) && !QFile::exists (userSvg))
{ // no user theme but a root one
ui->configLabel->setText (tr ("These are the settings that can be safely changed.<br>For the others, click <i>Save</i> and then edit this file:")
+ QString ("<br><i>~/.config/Kvantum/%1#/<b>%1#.kvconfig</b></i>").arg (kvconfigTheme_));
}
showAnimated (ui->configLabel, 1000);

if (kvconfigTheme_.endsWith ("#"))
ui->restoreButton->show();
Expand All @@ -1496,7 +1426,7 @@ void KvantumManager::tabChanged (int index)
theme config if this user theme doesn't have a config */
if (!QFile::exists (themeConfig))
{
if (hasUserSVG) // a user theme without config
if (QFile::exists (userSvg)) // a user theme without config
copyRootTheme (QString(), kvconfigTheme_);
else // a root theme
{
Expand Down Expand Up @@ -1797,7 +1727,10 @@ void KvantumManager::tabChanged (int index)
if (!confPageVisited_ )
{
confPageVisited_ = true;
fitThirdPageToContents();
/* a single-shot timer is needed for the layout to be fully calculated */
QTimer::singleShot (0, this, [this] {
fitConfPageToContents();
});
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions Kvantum/kvantummanager/KvantumManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ class KvantumManager : public QMainWindow
KvantumManager (const QString& lang = QString(), QWidget *parent = 0);
~KvantumManager();

void showWindow();

protected:
void closeEvent (QCloseEvent *event);
bool eventFilter (QObject *watched, QEvent *event);
Expand Down Expand Up @@ -72,7 +70,7 @@ private slots:
void setTabWidgetFocus();

private:
void fitThirdPageToContents();
void fitConfPageToContents();
QString tooTipToWhatsThis (const QString &tip);
void notWritable (const QString &path);
void canNotBeRemoved (const QString &path, bool isDir);
Expand All @@ -86,7 +84,6 @@ private slots:
void defaultThemeButtons();
void restyleWindow();
void writeOrigAppLists();
bool setConfigLabel (bool animate = false);
QString getComment (const QString &comboText, bool setState = true);
// to be independent of '../style/drag/windowmanager.h'
enum Drag {
Expand Down
Loading

0 comments on commit a5ec320

Please sign in to comment.