Skip to content

Commit

Permalink
Allow building without Qt Widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
Martchus committed Dec 11, 2024
1 parent 0285ce8 commit 3bb41ca
Show file tree
Hide file tree
Showing 14 changed files with 154 additions and 85 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ enable_testing()

# disable experimental Qt Quick GUI by default except on mobile platforms, enable libsyncthing by default
if (ANDROID OR IOS)
set(WIDGET_GUI_ENABLED_BY_DEFAULT OFF)
set(QUICK_GUI_ENABLED_BY_DEFAULT ON)
set(LIBSYNCTHING_DISABLED_BY_DEFAULT OFF)
else ()
set(WIDGET_GUI_ENABLED_BY_DEFAULT ON)
set(QUICK_GUI_ENABLED_BY_DEFAULT OFF)
set(LIBSYNCTHING_DISABLED_BY_DEFAULT ON)
endif ()
Expand Down Expand Up @@ -63,6 +65,7 @@ option(NO_FILE_ITEM_ACTION_PLUGIN "whether building the file item action plugin
option(NO_MODEL "whether building models should be skipped, implies NO_TRAY" OFF)
option(NO_WIDGETS "whether building widgets should be skipped, implies NO_TRAY" OFF)
option(NO_PLASMOID "whether building the Plasmoid for the Plasma desktop should be skipped" "${PLASMOID_DISABLED_BY_DEFAULT}")
option(WIDGETS_GUI "enables/disables building the Qt Widgets GUI (disabled by default)" ${WIDGET_GUI_ENABLED_BY_DEFAULT})
option(QUICK_GUI "enables/disables building the experimental Qt Quick GUI (disabled by default)" ${QUICK_GUI_ENABLED_BY_DEFAULT})

# allow using non-default configuration
Expand Down
14 changes: 4 additions & 10 deletions syncthingmodel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ set(META_APP_DESCRIPTION "Data models of Syncthing Tray")
set(META_PROJECT_VARNAME_UPPER LIB_SYNCTHING_MODEL)
set(META_PUBLIC_QT_MODULES Gui Widgets)
set(META_SRCDIR_REFS "${CMAKE_CURRENT_SOURCE_DIR}/../syncthingconnector")
set(META_GUI_OPTIONAL ON)

# add project files
set(HEADER_FILES
syncthingmodel.h
syncthingdirectorymodel.h
syncthingdevicemodel.h
syncthingdownloadmodel.h
syncthingerrormodel.h
syncthingfilemodel.h
syncthingrecentchangesmodel.h
Expand All @@ -27,14 +27,15 @@ set(SRC_FILES
syncthingmodel.cpp
syncthingdirectorymodel.cpp
syncthingdevicemodel.cpp
syncthingdownloadmodel.cpp
syncthingerrormodel.cpp
syncthingfilemodel.cpp
syncthingrecentchangesmodel.cpp
syncthingsortfiltermodel.cpp
syncthingstatuscomputionmodel.cpp
syncthingstatusselectionmodel.cpp
syncthingicons.cpp)
set(WIDGETS_HEADER_FILES syncthingdownloadmodel.h)
set(WIDGETS_SRC_FILES syncthingdownloadmodel.cpp)
set(RES_FILES resources/${META_PROJECT_NAME}icons.qrc)

set(TS_FILES translations/${META_PROJECT_NAME}_zh_CN.ts translations/${META_PROJECT_NAME}_cs_CZ.ts
Expand All @@ -61,14 +62,7 @@ find_package(${PACKAGE_NAMESPACE_PREFIX}qtforkawesome${CONFIGURATION_PACKAGE_SUF
use_qt_fork_awesome(VISIBILITY PUBLIC)

# link also explicitly against the following Qt modules
list(
APPEND
ADDITIONAL_QT_MODULES
Concurrent
Network
Gui
Widgets
Svg)
list(APPEND ADDITIONAL_QT_MODULES Concurrent Network Gui Svg)

# include modules to apply configuration
include(BasicConfig)
Expand Down
17 changes: 6 additions & 11 deletions syncthingwidgets/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ cmake_minimum_required(VERSION 3.17.0 FATAL_ERROR)
# metadata
set(META_PROJECT_NAME syncthingwidgets)
set(META_PROJECT_TYPE library)
set(META_APP_NAME "Widgets of Syncthing Tray")
set(META_APP_NAME "UI elements of Syncthing Tray")
set(META_PUBLIC_QT_MODULES Gui Widgets)
set(META_WEBVIEW_SRC_DIR webview)
set(META_GUI_OPTIONAL ON)

# add project files
set(HEADER_FILES misc/diffhighlighter.h misc/internalerror.h misc/statusinfo.h misc/syncthinglauncher.h misc/utils.h)
set(SRC_FILES misc/diffhighlighter.cpp misc/internalerror.cpp misc/statusinfo.cpp misc/syncthinglauncher.cpp misc/utils.cpp)
set(WIDGETS_HEADER_FILES
settings/settings.h
settings/settingsdialog.h
Expand All @@ -18,13 +21,9 @@ set(WIDGETS_HEADER_FILES
misc/textviewdialog.h
misc/internalerrorsdialog.h
misc/direrrorsdialog.h
misc/statusinfo.h
misc/dbusstatusnotifier.h
misc/internalerror.h
misc/otherdialogs.h
misc/syncthinglauncher.h
misc/syncthingkiller.h
misc/utils.h)
misc/syncthingkiller.h)
set(WIDGETS_SRC_FILES
settings/settings.cpp
settings/settingsdialog.cpp
Expand All @@ -38,13 +37,9 @@ set(WIDGETS_SRC_FILES
misc/textviewdialog.cpp
misc/internalerrorsdialog.cpp
misc/direrrorsdialog.cpp
misc/statusinfo.cpp
misc/dbusstatusnotifier.cpp
misc/internalerror.cpp
misc/otherdialogs.cpp
misc/syncthinglauncher.cpp
misc/syncthingkiller.cpp
misc/utils.cpp)
misc/syncthingkiller.cpp)
set(RES_FILES resources/${META_PROJECT_NAME}icons.qrc)
set(WIDGETS_UI_FILES
settings/connectionoptionpage.ui
Expand Down
37 changes: 37 additions & 0 deletions syncthingwidgets/misc/diffhighlighter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "./diffhighlighter.h"

#include <syncthingmodel/colors.h>

#include <QFont>
#include <QFontDatabase>
#include <QTextDocument>

using namespace std;
using namespace Data;

namespace QtGui {

DiffHighlighter::DiffHighlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
, m_enabled(true)
{
auto font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
m_baseFormat.setFont(font);

font.setBold(true);
m_addedFormat.setFont(font);
m_addedFormat.setForeground(Colors::green(true));
m_deletedFormat.setFont(font);
m_deletedFormat.setForeground(Colors::red(true));
}

void DiffHighlighter::highlightBlock(const QString &text)
{
if (text.startsWith(QChar('-'))) {
setFormat(0, static_cast<int>(text.size()), QColor(Qt::red));
} else if (text.startsWith(QChar('+'))) {
setFormat(0, static_cast<int>(text.size()), QColor(Qt::green));
}
}

} // namespace QtGui
39 changes: 39 additions & 0 deletions syncthingwidgets/misc/diffhighlighter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef SYNCTHINGWIDGETS_DIFF_HIGHLIGHTER_H
#define SYNCTHINGWIDGETS_DIFF_HIGHLIGHTER_H

#include "../global.h"

#include <QSyntaxHighlighter>
#include <QTextCharFormat>

namespace QtGui {

class SYNCTHINGWIDGETS_EXPORT DiffHighlighter : public QSyntaxHighlighter {
Q_OBJECT
Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled)
public:
explicit DiffHighlighter(QTextDocument *parent = nullptr);

bool isEnabled() const
{
return m_enabled;
}
void setEnabled(bool enabled)
{
if (enabled != m_enabled) {
m_enabled = enabled;
rehighlight();
}
}

protected:
void highlightBlock(const QString &text) override;

private:
QTextCharFormat m_baseFormat, m_addedFormat, m_deletedFormat;
bool m_enabled;
};

} // namespace QtGui

#endif // SYNCTHINGWIDGETS_DIFF_HIGHLIGHTER_H
8 changes: 8 additions & 0 deletions syncthingwidgets/misc/internalerror.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "./internalerror.h"
#include "./syncthinglauncher.h"

#if defined(SYNCTHINGWIDGETS_GUI_QTWIDGETS)
#include "../settings/settings.h"
#endif

#include <syncthingconnector/syncthingconnection.h>
#include <syncthingconnector/syncthingservice.h>
Expand All @@ -12,6 +14,7 @@ using namespace Data;

namespace QtGui {

#if defined(SYNCTHINGWIDGETS_GUI_QTWIDGETS)
/*!
* \brief Returns whether to ignore inavailability after start or standby-wakeup.
*/
Expand Down Expand Up @@ -61,6 +64,7 @@ static bool ignoreInavailabilityAfterStart(const Settings::Settings &settings, c
#endif
return false;
}
#endif

/*!
* \brief Returns whether the error is relevant. Only in this case a notification for the error should be shown.
Expand All @@ -78,6 +82,7 @@ bool InternalError::isRelevant(const SyncthingConnection &connection, SyncthingE
return true;
}

#if defined(SYNCTHINGWIDGETS_GUI_QTWIDGETS)
// ignore configuration errors on first launch (to avoid greeting people with an error message)
const auto &settings = Settings::values();
if ((settings.firstLaunch || settings.fakeFirstLaunch)
Expand Down Expand Up @@ -105,5 +110,8 @@ bool InternalError::isRelevant(const SyncthingConnection &connection, SyncthingE
service,
#endif
message, networkError);
#else
return true;
#endif
}
} // namespace QtGui
25 changes: 2 additions & 23 deletions syncthingwidgets/misc/otherdialogs.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "./otherdialogs.h"

#include "./diffhighlighter.h"
#include "./textviewdialog.h"

#include <syncthingconnector/syncthingconnection.h>
Expand Down Expand Up @@ -36,29 +38,6 @@ using namespace Data;

namespace QtGui {

DiffHighlighter::DiffHighlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
, m_enabled(true)
{
auto font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
m_baseFormat.setFont(font);

font.setBold(true);
m_addedFormat.setFont(font);
m_addedFormat.setForeground(Colors::green(true));
m_deletedFormat.setFont(font);
m_deletedFormat.setForeground(Colors::red(true));
}

void DiffHighlighter::highlightBlock(const QString &text)
{
if (text.startsWith(QChar('-'))) {
setFormat(0, static_cast<int>(text.size()), QColor(Qt::red));
} else if (text.startsWith(QChar('+'))) {
setFormat(0, static_cast<int>(text.size()), QColor(Qt::green));
}
}

static void setupOwnDeviceIdDialog(Data::SyncthingConnection &connection, int size, QWidget *dlg)
{
dlg->setWindowTitle(QCoreApplication::translate("QtGui::OtherDialogs", "Own device ID") + QStringLiteral(" - " APP_NAME));
Expand Down
29 changes: 0 additions & 29 deletions syncthingwidgets/misc/otherdialogs.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@

#include <QtGlobal>

#include <QSyntaxHighlighter>
#include <QTextCharFormat>

QT_FORWARD_DECLARE_CLASS(QDialog)
QT_FORWARD_DECLARE_CLASS(QWidget)

Expand All @@ -19,32 +16,6 @@ struct SyncthingDir;
namespace QtGui {
class TextViewDialog;

class SYNCTHINGWIDGETS_EXPORT DiffHighlighter : public QSyntaxHighlighter {
Q_OBJECT
Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled)
public:
explicit DiffHighlighter(QTextDocument *parent = nullptr);

bool isEnabled() const
{
return m_enabled;
}
void setEnabled(bool enabled)
{
if (enabled != m_enabled) {
m_enabled = enabled;
rehighlight();
}
}

protected:
void highlightBlock(const QString &text) override;

private:
QTextCharFormat m_baseFormat, m_addedFormat, m_deletedFormat;
bool m_enabled;
};

SYNCTHINGWIDGETS_EXPORT QDialog *ownDeviceIdDialog(Data::SyncthingConnection &connection);
SYNCTHINGWIDGETS_EXPORT QWidget *ownDeviceIdWidget(Data::SyncthingConnection &connection, int size, QWidget *parent = nullptr);
SYNCTHINGWIDGETS_EXPORT QDialog *browseRemoteFilesDialog(
Expand Down
22 changes: 19 additions & 3 deletions syncthingwidgets/misc/syncthinglauncher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include <syncthingconnector/syncthingconnection.h>
#include <syncthingconnector/utils.h>

#if defined(SYNCTHINGWIDGETS_GUI_QTWIDGETS)
#include "../settings/settings.h"
#endif

#include <c++utilities/io/ansiescapecodes.h>

Expand Down Expand Up @@ -41,7 +43,9 @@ SyncthingLauncher *SyncthingLauncher::s_mainInstance = nullptr;
*/
SyncthingLauncher::SyncthingLauncher(QObject *parent)
: QObject(parent)
#if defined(SYNCTHINGWIDGETS_GUI_QTWIDGETS)
, m_lastLauncherSettings(nullptr)
#endif
, m_relevantConnection(nullptr)
, m_guiListeningUrlSearch("Access the GUI via the following URL: ", "\n\r", std::string_view(), BufferSearch::CallbackType())
, m_exitSearch("Syncthing exited: ", "\n\r", std::string_view(), BufferSearch::CallbackType())
Expand Down Expand Up @@ -92,7 +96,9 @@ void SyncthingLauncher::setRunning(bool running, LibSyncthing::RuntimeOptions &&
tearDownLibSyncthing();
}
// save runtime options so Syncthing can resume in case runtime conditions allow it
#if defined(SYNCTHINGWIDGETS_GUI_QTWIDGETS)
m_lastLauncherSettings = nullptr;
#endif
m_lastRuntimeOptions = std::move(runtimeOptions);
// emit signal in any case (even if there's no change) so runningStatus() is re-evaluated
emit runningChanged(shouldBeRunning);
Expand Down Expand Up @@ -155,13 +161,19 @@ void SyncthingLauncher::setNetworkConnectionMetered(std::optional<bool> metered)
if (metered.value_or(false)) {
terminateDueToMeteredConnection();
} else if (!metered.value_or(true) && m_stoppedMetered) {
#if defined(SYNCTHINGWIDGETS_GUI_QTWIDGETS)
if (m_lastLauncherSettings) {
launch(*m_lastLauncherSettings);
#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
} else if (m_lastRuntimeOptions) {
launch(*m_lastRuntimeOptions);
}
#endif
#if defined(SYNCTHINGWIDGETS_GUI_QTWIDGETS) && defined(SYNCTHINGWIDGETS_USE_LIBSYNCTHING)
else
#endif
#if defined(SYNCTHINGWIDGETS_USE_LIBSYNCTHING)
if (m_lastRuntimeOptions) {
launch(*m_lastRuntimeOptions);
}
#endif
}
}
emit networkConnectionMeteredChanged(metered);
Expand Down Expand Up @@ -231,6 +243,7 @@ void SyncthingLauncher::launch(const QString &program, const QStringList &argume
#endif
}

#if defined(SYNCTHINGWIDGETS_GUI_QTWIDGETS)
/*!
* \brief Launches a Syncthing instance according to the specified \a launcherSettings.
* \remarks Does nothing if already running an instance.
Expand Down Expand Up @@ -267,6 +280,7 @@ void SyncthingLauncher::launch(const Settings::Launcher &launcherSettings)
m_lastRuntimeOptions.reset();
#endif
}
#endif

#ifdef SYNCTHINGWIDGETS_USE_LIBSYNCTHING
/*!
Expand Down Expand Up @@ -438,9 +452,11 @@ void SyncthingLauncher::terminateDueToMeteredConnection()
// running before)
return;
}
#if defined(SYNCTHINGWIDGETS_GUI_QTWIDGETS)
if (m_lastLauncherSettings && !m_relevantConnection) {
m_relevantConnection = m_lastLauncherSettings->connectionForLauncher(this);
}
#endif
terminate(m_relevantConnection);
m_stoppedMetered = true;
}
Expand Down
Loading

0 comments on commit 3bb41ca

Please sign in to comment.