mirror of
https://github.com/kiwix/kiwix-desktop.git
synced 2025-09-22 03:26:05 -04:00
port settings to Qt Widget
This commit is contained in:
parent
c65bb5042c
commit
93854011eb
@ -44,7 +44,7 @@ SOURCES += \
|
||||
src/blobbuffer.cpp \
|
||||
src/library.cpp \
|
||||
src/settingsmanager.cpp \
|
||||
src/settingsmanagerview.cpp \
|
||||
src/settingsview.cpp \
|
||||
src/topwidget.cpp \
|
||||
src/urlschemehandler.cpp \
|
||||
src/webview.cpp \
|
||||
@ -76,7 +76,7 @@ HEADERS += \
|
||||
src/blobbuffer.h \
|
||||
src/library.h \
|
||||
src/settingsmanager.h \
|
||||
src/settingsmanagerview.h \
|
||||
src/settingsview.h \
|
||||
src/topwidget.h \
|
||||
src/kconstants.h \
|
||||
src/urlschemehandler.h \
|
||||
|
@ -17,7 +17,6 @@
|
||||
|
||||
KiwixApp::KiwixApp(int& argc, char *argv[])
|
||||
: QtSingleApplication("kiwix-desktop", argc, argv),
|
||||
m_settingsManager(),
|
||||
m_profile(),
|
||||
m_libraryDirectory(findLibraryDirectory()),
|
||||
m_library(m_libraryDirectory),
|
||||
|
@ -13,12 +13,18 @@ SettingsManager::SettingsManager(QObject *parent)
|
||||
initSettings();
|
||||
}
|
||||
|
||||
SettingsManagerView* SettingsManager::getView()
|
||||
SettingsView* SettingsManager::getView()
|
||||
{
|
||||
auto view = new SettingsManagerView();
|
||||
view->registerObject("settingsManager", this);
|
||||
view->setHtml();
|
||||
connect(view, &QObject::destroyed, this, [=]() { m_settingsViewDisplayed = false; });
|
||||
static SettingsView *view = nullptr;
|
||||
|
||||
if (!view || !m_settingsViewDisplayed)
|
||||
view = new SettingsView();
|
||||
|
||||
view->init(m_kiwixServerPort, m_zoomFactor * 100, m_downloadDir);
|
||||
connect(view, &QObject::destroyed, this, [=]() { m_settingsViewDisplayed = false;});
|
||||
connect(view, &SettingsView::serverPortChanged, this, &SettingsManager::setKiwixServerPort);
|
||||
connect(view, &SettingsView::downloadDirChanged, this, &SettingsManager::setDownloadDir);
|
||||
connect(view, &SettingsView::zoomFactorChanged, this, &SettingsManager::setZoomFactor);
|
||||
m_settingsViewDisplayed = true;
|
||||
return view;
|
||||
}
|
||||
@ -62,6 +68,9 @@ void SettingsManager::setKiwixServerPort(int port)
|
||||
|
||||
void SettingsManager::setZoomFactor(qreal zoomFactor)
|
||||
{
|
||||
if (zoomFactor > 1)
|
||||
zoomFactor /= 100;
|
||||
|
||||
m_zoomFactor = zoomFactor;
|
||||
m_settings.setValue("view/zoomFactor", zoomFactor);
|
||||
}
|
||||
|
@ -3,7 +3,7 @@
|
||||
|
||||
#include <QObject>
|
||||
#include <QSettings>
|
||||
#include "settingsmanagerview.h"
|
||||
#include "settingsview.h"
|
||||
|
||||
class SettingsManager : public QObject
|
||||
{
|
||||
@ -16,7 +16,7 @@ public:
|
||||
explicit SettingsManager(QObject *parent = nullptr);
|
||||
virtual ~SettingsManager() {};
|
||||
|
||||
SettingsManagerView* getView();
|
||||
SettingsView* getView();
|
||||
bool isSettingsViewdisplayed() { return m_settingsViewDisplayed; };
|
||||
void setSettings(const QString &key, const QVariant &value);
|
||||
void deleteSettings(const QString &key);
|
||||
|
@ -1,23 +0,0 @@
|
||||
#include "settingsmanagerview.h"
|
||||
#include "kiwixapp.h"
|
||||
#include <QFile>
|
||||
|
||||
SettingsManagerView::SettingsManagerView(QWidget *parent) : QWebEngineView(parent)
|
||||
{
|
||||
page()->setWebChannel(&m_webChannel);
|
||||
setContextMenuPolicy( Qt::NoContextMenu );
|
||||
}
|
||||
|
||||
void SettingsManagerView::registerObject(const QString& id, QObject* object)
|
||||
{
|
||||
m_webChannel.registerObject(id, object);
|
||||
}
|
||||
|
||||
void SettingsManagerView::setHtml()
|
||||
{
|
||||
QFile contentFile(":texts/_settingsManager.html");
|
||||
contentFile.open(QIODevice::ReadOnly);
|
||||
auto byteContent = contentFile.readAll();
|
||||
contentFile.close();
|
||||
QWebEngineView::setHtml(byteContent);
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
#ifndef SETTINGSMANAGERVIEW_H
|
||||
#define SETTINGSMANAGERVIEW_H
|
||||
|
||||
#include <QWebEngineView>
|
||||
#include <QWebChannel>
|
||||
|
||||
class SettingsManagerView : public QWebEngineView
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
SettingsManagerView(QWidget *parent = nullptr);
|
||||
void registerObject(const QString &id, QObject *object);
|
||||
void setHtml();
|
||||
|
||||
private:
|
||||
QWebChannel m_webChannel;
|
||||
};
|
||||
|
||||
#endif // SETTINGSMANAGERVIEW_H
|
73
src/settingsview.cpp
Normal file
73
src/settingsview.cpp
Normal file
@ -0,0 +1,73 @@
|
||||
#include "settingsview.h"
|
||||
#include "ui_settings.h"
|
||||
#include "kiwixapp.h"
|
||||
|
||||
#include <QMessageBox>
|
||||
#include <QFileDialog>
|
||||
SettingsView::SettingsView(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
, ui(new Ui::Settings)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
connect(ui->serverPortSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &SettingsView::serverPortChanged);
|
||||
connect(ui->zoomLevelSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &SettingsView::zoomFactorChanged);
|
||||
connect(ui->resetButton, &QPushButton::clicked, this, &SettingsView::resetDownloadDir);
|
||||
connect(ui->browseButton, &QPushButton::clicked, this, &SettingsView::browseDownloadDir);
|
||||
ui->settingsLabel->setText(gt("settings"));
|
||||
ui->serverPortLabel->setText(gt("port-for-local-kiwix-server-setting"));
|
||||
ui->zoomLevelLabel->setText(gt("zoom-level-setting"));
|
||||
ui->downloadDirLabel->setText(gt("download-directory-setting"));
|
||||
ui->resetButton->setText(gt("reset"));
|
||||
ui->browseButton->setText(gt("browse"));
|
||||
}
|
||||
void SettingsView::init(int port, int factor, const QString &dir)
|
||||
{
|
||||
ui->serverPortSpinBox->setValue(port);
|
||||
ui->zoomLevelSpinBox->setValue(factor);
|
||||
ui->downloadDirPath->setText(dir);
|
||||
}
|
||||
bool SettingsView::confirmDialogDownloadDir(const QString& dir)
|
||||
{
|
||||
auto text = gt("download-dir-dialog-msg");
|
||||
text = text.replace("{{DIRECTORY}}", dir);
|
||||
QMessageBox msgBox(
|
||||
QMessageBox::Question, //Icon
|
||||
gt("download-dir-dialog-title"), //Title
|
||||
text, //Text
|
||||
QMessageBox::Ok | QMessageBox::Cancel //Buttons
|
||||
);
|
||||
msgBox.setDefaultButton(QMessageBox::Ok);
|
||||
|
||||
int ret = msgBox.exec();
|
||||
return (ret == QMessageBox::Ok);
|
||||
}
|
||||
|
||||
void SettingsView::resetDownloadDir()
|
||||
{
|
||||
auto dir = QString::fromStdString(getDataDirectory());
|
||||
const auto &downloadDir = KiwixApp::instance()->getSettingsManager()->getDownloadDir();
|
||||
if (dir == downloadDir) {
|
||||
return;
|
||||
}
|
||||
if (confirmDialogDownloadDir(dir)) {
|
||||
ui->downloadDirPath->setText(dir);
|
||||
emit(downloadDirChanged(dir));
|
||||
}
|
||||
}
|
||||
|
||||
void SettingsView::browseDownloadDir()
|
||||
{
|
||||
const auto &downloadDir = KiwixApp::instance()->getSettingsManager()->getDownloadDir();
|
||||
QString dir = QFileDialog::getExistingDirectory(KiwixApp::instance()->getMainWindow(),
|
||||
gt("browse-directory"),
|
||||
downloadDir,
|
||||
QFileDialog::ShowDirsOnly);
|
||||
if (dir == downloadDir || dir.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (confirmDialogDownloadDir(dir)) {
|
||||
ui->downloadDirPath->setText(dir);
|
||||
emit(downloadDirChanged(dir));
|
||||
}
|
||||
}
|
28
src/settingsview.h
Normal file
28
src/settingsview.h
Normal file
@ -0,0 +1,28 @@
|
||||
#ifndef SETTINGSVIEW_H
|
||||
#define SETTINGSVIEW_H
|
||||
|
||||
#include <QWidget>
|
||||
namespace Ui {
|
||||
class Settings;
|
||||
}
|
||||
class SettingsView : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
SettingsView(QWidget *parent = nullptr);
|
||||
~SettingsView(){};
|
||||
void init(int port, int factor, const QString &dir);
|
||||
public Q_SLOTS:
|
||||
void resetDownloadDir();
|
||||
void browseDownloadDir();
|
||||
signals:
|
||||
void serverPortChanged(int port);
|
||||
void zoomFactorChanged(int factor);
|
||||
void downloadDirChanged(const QString &dir);
|
||||
private:
|
||||
bool confirmDialogDownloadDir(const QString& dir);
|
||||
|
||||
Ui::Settings *ui;
|
||||
};
|
||||
|
||||
#endif // SETTINGSVIEW_H
|
@ -56,13 +56,13 @@ TabBar::TabBar(QWidget *parent) :
|
||||
connect(app->getAction(KiwixApp::SettingAction), &QAction::triggered,
|
||||
this, [=]() {
|
||||
for (int i = 0 ; i < (mp_stackedWidget->count() - 1) ; i++) {
|
||||
if (qobject_cast<SettingsManagerView*>(mp_stackedWidget->widget(i))) {
|
||||
if (qobject_cast<SettingsView*>(mp_stackedWidget->widget(i))) {
|
||||
setCurrentIndex(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
int index = currentIndex() + 1;
|
||||
SettingsManagerView* view = KiwixApp::instance()->getSettingsManager()->getView();
|
||||
SettingsView* view = KiwixApp::instance()->getSettingsManager()->getView();
|
||||
mp_stackedWidget->insertWidget(index, view);
|
||||
insertTab(index,QIcon(":/icons/settings.svg"), gt("settings"));
|
||||
QToolButton *tb = new QToolButton(this);
|
||||
@ -294,7 +294,7 @@ void TabBar::onCurrentChanged(int index)
|
||||
|
||||
QWidget *w = mp_stackedWidget->widget(index);
|
||||
|
||||
if (qobject_cast<SettingsManagerView*>(w)) {
|
||||
if (qobject_cast<SettingsView*>(w)) {
|
||||
emit webActionEnabledChanged(QWebEnginePage::Back, false);
|
||||
emit webActionEnabledChanged(QWebEnginePage::Forward, false);
|
||||
emit libraryPageDisplayed(false);
|
||||
|
241
ui/settings.ui
Normal file
241
ui/settings.ui
Normal file
@ -0,0 +1,241 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>Settings</class>
|
||||
<widget class="QWidget" name="Settings">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>1029</width>
|
||||
<height>580</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>600</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Form</string>
|
||||
</property>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_4">
|
||||
<item>
|
||||
<widget class="QWidget" name="widget" native="true">
|
||||
<property name="maximumSize">
|
||||
<size>
|
||||
<width>600</width>
|
||||
<height>16777215</height>
|
||||
</size>
|
||||
</property>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_5">
|
||||
<item>
|
||||
<layout class="QVBoxLayout" name="mainLayout">
|
||||
<item>
|
||||
<widget class="QLabel" name="settingsLabel">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>0</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="font">
|
||||
<font>
|
||||
<pointsize>16</pointsize>
|
||||
<weight>75</weight>
|
||||
<bold>true</bold>
|
||||
</font>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Settings</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="Line" name="line_3">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<item>
|
||||
<widget class="QLabel" name="serverPortLabel">
|
||||
<property name="font">
|
||||
<font>
|
||||
<pointsize>10</pointsize>
|
||||
<weight>75</weight>
|
||||
<bold>true</bold>
|
||||
</font>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Port for local kiwix server:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="horizontalSpacer_3">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>40</width>
|
||||
<height>20</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QSpinBox" name="serverPortSpinBox">
|
||||
<property name="suffix">
|
||||
<string/>
|
||||
</property>
|
||||
<property name="prefix">
|
||||
<string/>
|
||||
</property>
|
||||
<property name="minimum">
|
||||
<number>1</number>
|
||||
</property>
|
||||
<property name="maximum">
|
||||
<number>65535</number>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="Line" name="line">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
||||
<item>
|
||||
<widget class="QLabel" name="zoomLevelLabel">
|
||||
<property name="font">
|
||||
<font>
|
||||
<weight>75</weight>
|
||||
<bold>true</bold>
|
||||
</font>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Zoom level:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="horizontalSpacer_4">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>40</width>
|
||||
<height>20</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QSpinBox" name="zoomLevelSpinBox">
|
||||
<property name="suffix">
|
||||
<string>%</string>
|
||||
</property>
|
||||
<property name="maximum">
|
||||
<number>100</number>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="Line" name="line_2">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_3">
|
||||
<item>
|
||||
<widget class="QLabel" name="downloadDirLabel">
|
||||
<property name="font">
|
||||
<font>
|
||||
<weight>75</weight>
|
||||
<bold>true</bold>
|
||||
</font>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Download directory:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="horizontalSpacer_5">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>40</width>
|
||||
<height>20</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="downloadDirPath">
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="resetButton">
|
||||
<property name="text">
|
||||
<string>Reset</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="browseButton">
|
||||
<property name="text">
|
||||
<string>Browse</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="verticalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Vertical</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>20</width>
|
||||
<height>40</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
Loading…
x
Reference in New Issue
Block a user