Skip to content

Commit

Permalink
Allow stylesheet to modify base style.
Browse files Browse the repository at this point in the history
  • Loading branch information
Holt59 committed Jul 17, 2024
1 parent 7095d10 commit 30b8d23
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 11 deletions.
82 changes: 80 additions & 2 deletions src/moapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>.
#include <iplugingame.h>
#include <log.h>
#include <report.h>
#include <scopeguard.h>
#include <utility.h>

// see addDllsToPath() below
Expand Down Expand Up @@ -558,14 +559,91 @@ bool MOApplication::notify(QObject* receiver, QEvent* event)
}
}

namespace
{
QStringList extractTopStyleSheetComments(QFile& stylesheet)
{
if (!stylesheet.open(QFile::ReadOnly)) {
log::error("failed to open stylesheet file {}", stylesheet.fileName());
return {};
}
ON_BLOCK_EXIT([&stylesheet]() {
stylesheet.close();
});

QStringList topComments;

while (true) {
const auto byteLine = stylesheet.readLine();
if (byteLine.isNull()) {
break;
}

const auto line = QString(byteLine).trimmed();

// skip empty lines
if (line.isEmpty()) {
continue;
}

// only handle single line comments
if (!line.startsWith("/*")) {
break;
}

topComments.push_back(line.mid(2, line.size() - 4).trimmed());
}

return topComments;
}

QString extractBaseStyleFromStyleSheet(QFile& stylesheet, const QString& defaultStyle)
{
// read the first line of the files that are either empty or comments
//
const auto topLines = extractTopStyleSheetComments(stylesheet);

QString style = defaultStyle;

for (const auto& line : topLines) {
if (!line.startsWith("mo2-base-style")) {
continue;
}

const auto parts = line.split(":");
if (parts.size() != 2) {
log::warn("found invalid top-comment for mo2 in {}: {}", stylesheet.fileName(),
line);
continue;
}

const auto tmpStyle = parts[1].trimmed();
if (QStyleFactory::keys().count(tmpStyle) == 0) {
log::warn("base style '{}' from style '{}' not found", tmpStyle,
stylesheet.fileName(), line);
continue;
}

log::info("found base style '{}' for style '{}'", tmpStyle, stylesheet.fileName());
style = tmpStyle;
break;
}

return style;
}

} // namespace

void MOApplication::updateStyle(const QString& fileName)
{
if (QStyleFactory::keys().contains(fileName)) {
setStyleSheet("");
setStyle(new ProxyStyle(QStyleFactory::create(fileName)));
} else {
setStyle(new ProxyStyle(QStyleFactory::create(m_defaultStyle)));
if (QFile::exists(fileName)) {
QFile stylesheet(fileName);
if (stylesheet.exists()) {
setStyle(new ProxyStyle(QStyleFactory::create(
extractBaseStyleFromStyleSheet(stylesheet, m_defaultStyle))));
setStyleSheet(QString("file:///%1").arg(fileName));
} else {
log::warn("invalid stylesheet: {}", fileName);
Expand Down
18 changes: 9 additions & 9 deletions src/organizer_en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2901,37 +2901,37 @@ This is likely due to a corrupted or incompatible download or unrecognized archi
<context>
<name>MOApplication</name>
<message>
<location filename="moapplication.cpp" line="197"/>
<location filename="moapplication.cpp" line="198"/>
<source>Failed to create log folder.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="moapplication.cpp" line="261"/>
<location filename="moapplication.cpp" line="262"/>
<source>Failed to set up data paths.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="moapplication.cpp" line="392"/>
<location filename="moapplication.cpp" line="393"/>
<source>Download started</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="moapplication.cpp" line="409"/>
<location filename="moapplication.cpp" line="410"/>
<source>This shortcut or command line is for instance &apos;%1&apos;, but the current instance is &apos;%2&apos;.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="moapplication.cpp" line="421"/>
<location filename="moapplication.cpp" line="422"/>
<source>This shortcut or command line is for profile &apos;%1&apos;, but the current profile is &apos;%2&apos;.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="moapplication.cpp" line="551"/>
<location filename="moapplication.cpp" line="552"/>
<source>an error occurred: %1</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="moapplication.cpp" line="556"/>
<location filename="moapplication.cpp" line="557"/>
<source>an error occurred</source>
<translation type="unfinished"></translation>
</message>
Expand Down Expand Up @@ -7639,12 +7639,12 @@ Destination:<byte value="xd"/>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="moapplication.cpp" line="451"/>
<location filename="moapplication.cpp" line="452"/>
<source>Instance at &apos;%1&apos; not found. Select another instance.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="moapplication.cpp" line="455"/>
<location filename="moapplication.cpp" line="456"/>
<source>Instance at &apos;%1&apos; not found. You must create a new instance</source>
<translation type="unfinished"></translation>
</message>
Expand Down

0 comments on commit 30b8d23

Please sign in to comment.