/* * Common Carla code * Copyright (C) 2011-2019 Filipe Coelho * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * For a full copy of the GNU General Public License see the doc/GPL.txt file. */ #include "carla_shared.hpp" //--------------------------------------------------------------------------------------------------------------------- // Imports (Global) #include #include #include #include //--------------------------------------------------------------------------------------------------------------------- // Imports (Custom) #include "CarlaMathUtils.hpp" //--------------------------------------------------------------------------------------------------------------------- // Global Carla object CarlaObject::CarlaObject() noexcept : gui(nullptr), nogui(false), term(false) {} CarlaObject gCarla; //--------------------------------------------------------------------------------------------------------------------- // Get Icon from user theme, using our own as backup (Oxygen) QIcon getIcon(const QString icon, const int size) { return QIcon::fromTheme(icon, QIcon(QString(":/%1x%1/%2.png").arg(size).arg(icon))); } //--------------------------------------------------------------------------------------------------------------------- // Handle some basic command-line arguments shared between all carla variants QString handleInitialCommandLineArguments(const int argc, char* argv[]) { static const QStringList listArgsNoGUI = { "-n", "--n", "-no-gui", "--no-gui", "-nogui", "--nogui" }; static const QStringList listArgsHelp = { "-h", "--h", "-help", "--help" }; static const QStringList listArgsVersion = { "-v", "--v", "-version", "--version" }; QString initName(argv[0]); // = os.path.basename(file) if (file is not None and os.path.dirname(file) in PATH) else sys.argv[0] // libPrefix = None for (int i=1; itr("Set Path"), lineEdit->text(), QFileDialog::ShowDirsOnly); if (newPath.isNotEmpty()) lineEdit->setText(newPath); return newPath; } //--------------------------------------------------------------------------------------------------------------------- // fill up a qlists from a C arrays void fillQStringListFromStringArray(QStringList& list, const char* const* const stringArray) { uint count = 0; // count number of strings first for (; stringArray[count] != nullptr; ++count) {} // allocate list list.reserve(count); // fill in strings for (count = 0; stringArray[count] != nullptr; ++count) list.append(stringArray[count]); } void fillQDoubleListFromDoubleArray(QList& list, const double* const doubleArray) { uint count = 0; // count number of strings first for (; carla_isNotZero(doubleArray[count]); ++count) {} // allocate list list.reserve(count); // fill in strings for (count = 0; carla_isNotZero(doubleArray[count]); ++count) list.append(doubleArray[count]); } void fillQUIntListFromUIntArray(QList& list, const uint* const uintArray) { uint count = 0; // count number of strings first for (; uintArray[count] != 0; ++count) {} // allocate list list.reserve(count); // fill in strings for (count = 0; uintArray[count] != 0; ++count) list.append(uintArray[count]); } //--------------------------------------------------------------------------------------------------------------------- // Backwards-compatible horizontalAdvance/width call, depending on qt version int fontMetricsHorizontalAdvance(const QFontMetrics& fm, const QString& s) { #if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)) return fm.horizontalAdvance(s); #else return fm.width(s); #endif } //--------------------------------------------------------------------------------------------------------------------- // Check if a string array contains a string bool stringArrayContainsString(const char* const* const stringArray, const char* const string) noexcept { for (uint i=0; stringArray[i] != nullptr; ++i) { if (std::strcmp(stringArray[i], string) == 0) return true; } return false; } //--------------------------------------------------------------------------------------------------------------------- // Custom QMessageBox which resizes itself to fit text void QMessageBoxWithBetterWidth::showEvent(QShowEvent* const event) { const QFontMetrics metrics(fontMetrics()); const QStringList lines(text().trimmed().split("\n") + informativeText().trimmed().split("\n")); if (lines.size() > 0) { int width = 0; for (const QString& line : lines) width = std::max(fontMetricsHorizontalAdvance(metrics, line), width); if (QGridLayout* const layout_ = dynamic_cast(layout())) layout_->setColumnMinimumWidth(2, width + 12); } QMessageBox::showEvent(event); } //--------------------------------------------------------------------------------------------------------------------- // Safer QSettings class, which does not throw if type mismatches bool QSafeSettings::valueBool(const QString key, const bool defaultValue) const { QVariant var(value(key, defaultValue)); CARLA_SAFE_ASSERT_RETURN(var.convert(QVariant::Bool), defaultValue); return var.isValid() ? var.toBool() : defaultValue; } Qt::CheckState QSafeSettings::valueCheckState(const QString key, const Qt::CheckState defaultValue) const { QVariant var(value(key, defaultValue)); CARLA_SAFE_ASSERT_RETURN(var.convert(QVariant::UInt), defaultValue); if (! var.isValid()) return defaultValue; const uint value = var.toUInt(); switch (value) { case Qt::Unchecked: case Qt::PartiallyChecked: case Qt::Checked: return static_cast(value); default: return defaultValue; } } uint QSafeSettings::valueUInt(const QString key, const uint defaultValue) const { QVariant var(value(key, defaultValue)); CARLA_SAFE_ASSERT_RETURN(var.convert(QVariant::UInt), defaultValue); return var.isValid() ? var.toUInt() : defaultValue; } double QSafeSettings::valueDouble(const QString key, const double defaultValue) const { QVariant var(value(key, defaultValue)); CARLA_SAFE_ASSERT_RETURN(var.convert(QVariant::Double), defaultValue); return var.isValid() ? var.toDouble() : defaultValue; } QString QSafeSettings::valueString(const QString key, const QString defaultValue) const { QVariant var(value(key, defaultValue)); CARLA_SAFE_ASSERT_RETURN(var.convert(QVariant::String), defaultValue); return var.isValid() ? var.toString() : defaultValue; } QByteArray QSafeSettings::valueByteArray(const QString key, const QByteArray defaultValue) const { QVariant var(value(key, defaultValue)); CARLA_SAFE_ASSERT_RETURN(var.convert(QVariant::ByteArray), defaultValue); return var.isValid() ? var.toByteArray() : defaultValue; } QStringList QSafeSettings::valueStringList(const QString key, const QStringList defaultValue) const { QVariant var(value(key, defaultValue)); CARLA_SAFE_ASSERT_RETURN(var.convert(QVariant::StringList), defaultValue); return var.isValid() ? var.toStringList() : defaultValue; } //--------------------------------------------------------------------------------------------------------------------- // Custom MessageBox int CustomMessageBox(QWidget* const parent, const QMessageBox::Icon icon, const QString title, const QString text, const QString extraText, const QMessageBox::StandardButtons buttons, const QMessageBox::StandardButton defButton) { QMessageBoxWithBetterWidth msgBox(parent); msgBox.setIcon(icon); msgBox.setWindowTitle(title); msgBox.setText(text); msgBox.setInformativeText(extraText); msgBox.setStandardButtons(buttons); msgBox.setDefaultButton(defButton); return msgBox.exec(); } //---------------------------------------------------------------------------------------------------------------------