From 3fc91bded5d8f899703d42206f632d4edd12a96f Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sun, 19 Aug 2018 16:18:07 +0200 Subject: [PATCH] Move some helper functions to Helpers.hpp --- src/util/Helpers.hpp | 37 +++++++++++++++++++ .../settingspages/ExternalToolsPage.cpp | 13 ++----- src/widgets/settingspages/ModerationPage.cpp | 31 +++------------- 3 files changed, 45 insertions(+), 36 deletions(-) diff --git a/src/util/Helpers.hpp b/src/util/Helpers.hpp index 1552ae404..bb6c742a2 100644 --- a/src/util/Helpers.hpp +++ b/src/util/Helpers.hpp @@ -17,6 +17,43 @@ static QString CreateUUID() return uuid.toString(); } +static QString createLink(const QString &url, bool file = false) +{ + if (file) { + return QString("" + url + + ""); + } + + return QString("" + + url + ""); +} + +static QString createNamedLink(const QString &url, const QString &name, bool file = false) +{ + if (file) { + return QString("" + name + + ""); + } + + return QString("" + + name + ""); +} + +static QString shortenString(const QString &str, unsigned maxWidth = 50) +{ + if (str.size() <= maxWidth) { + return str; + } + + QString shortenedStr = str; + shortenedStr.resize(47); + shortenedStr += "..."; + + return shortenedStr; +} + } // namespace chatterino namespace fmt { diff --git a/src/widgets/settingspages/ExternalToolsPage.cpp b/src/widgets/settingspages/ExternalToolsPage.cpp index 49204d9cb..c57768528 100644 --- a/src/widgets/settingspages/ExternalToolsPage.cpp +++ b/src/widgets/settingspages/ExternalToolsPage.cpp @@ -1,6 +1,7 @@ #include "ExternalToolsPage.hpp" #include "Application.hpp" +#include "util/Helpers.hpp" #include "util/LayoutCreator.hpp" #include @@ -9,14 +10,6 @@ "Choose", "Source", "High", "Medium", "Low", "Audio only" namespace chatterino { -namespace { - QString createLink(const QString &url, const QString &name) - { - return QString("" + name + - ""); - } -} // namespace ExternalToolsPage::ExternalToolsPage() : SettingsPage("External tools", "") @@ -39,8 +32,8 @@ ExternalToolsPage::ExternalToolsPage() description->setStyleSheet("color: #bbb"); auto links = new QLabel( - createLink("https://streamlink.github.io/", "Website") + " " + - createLink( + createNamedLink("https://streamlink.github.io/", "Website") + " " + + createNamedLink( "https://github.com/streamlink/streamlink/releases/latest", "Download")); links->setTextFormat(Qt::RichText); diff --git a/src/widgets/settingspages/ModerationPage.cpp b/src/widgets/settingspages/ModerationPage.cpp index 89d6b2411..e7210f5a6 100644 --- a/src/widgets/settingspages/ModerationPage.cpp +++ b/src/widgets/settingspages/ModerationPage.cpp @@ -7,6 +7,7 @@ #include "controllers/taggedusers/TaggedUsersModel.hpp" #include "singletons/Logging.hpp" #include "singletons/Paths.hpp" +#include "util/Helpers.hpp" #include "util/LayoutCreator.hpp" #include "widgets/helper/EditableModelView.hpp" @@ -25,18 +26,6 @@ namespace chatterino { -inline QString createLink(const QString &url, bool file = false) -{ - if (file) { - return QString("" + url + - ""); - } - - return QString("" + - url + ""); -} - qint64 dirSize(QString dirPath) { qint64 size = 0; @@ -110,20 +99,10 @@ ModerationPage::ModerationPage() pathOriginal = logPath; } - QString pathShortened; - - if (pathOriginal.size() > 50) { - pathShortened = pathOriginal; - pathShortened.resize(50); - pathShortened += "..."; - } else { - pathShortened = pathOriginal; - } - - pathShortened = "Logs saved at " + - pathShortened + ""; + QString pathShortened = + "Logs saved at " + + shortenString(pathOriginal, 50) + ""; logsPathLabel->setText(pathShortened); logsPathLabel->setToolTip(pathOriginal);