Files
qmltermwidget/lib/tools.cpp
T
Luís Pereira 3ce71a3650 Fixes (#122)
* Prevent a possible C++11 range for detach

The solution with Qt>=5.7 is to use the qAsConst() macro.
But the qAsConst macro is just a const_cast to const T&.

* Don't call QByteArray::operator[]() on temporary

Just use the QByteArray::at().

* Adds missing reference in foreach

That's an non non trivial type (QString), using a reference as no drawbacks
and it performs better.

* Use QStringList::constFirst()

Drop QStringList::first(). It's faster and we might avoid detaching a
temporary.

* Don't call QList::operator[]() on temporary objects

It's not what we want. The at operator and QList::value() do the job in the
right way.

* Stops allocating an unneeded temporary container

We were allocating an temporary container (_entries.values(keyCode)) which
implies an extra iteration also.
Now we don't use any temporary container and only perform one iteration.
2017-04-22 11:55:16 +02:00

111 lines
2.9 KiB
C++

#include "tools.h"
#include <QCoreApplication>
#include <QDir>
#include <QtDebug>
/*! Helper function to get possible location of layout files.
By default the KB_LAYOUT_DIR is used (linux/BSD/macports).
But in some cases (apple bundle) there can be more locations).
*/
QString get_kb_layout_dir()
{
#ifdef BUNDLE_KEYBOARDLAYOUTS
return QLatin1String(":/");
#else
// qDebug() << __FILE__ << __FUNCTION__;
QString rval = "";
QString k(KB_LAYOUT_DIR);
QDir d(k);
qDebug() << "default KB_LAYOUT_DIR: " << k;
if (d.exists())
{
rval = k.append("/");
return rval;
}
// subdir in the app location
d.setPath(QCoreApplication::applicationDirPath() + "/kb-layouts/");
//qDebug() << d.path();
if (d.exists())
return QCoreApplication::applicationDirPath() + "/kb-layouts/";
#ifdef Q_WS_MAC
d.setPath(QCoreApplication::applicationDirPath() + "/../Resources/kb-layouts/");
if (d.exists())
return QCoreApplication::applicationDirPath() + "/../Resources/kb-layouts/";
#endif
qDebug() << "Cannot find KB_LAYOUT_DIR. Default:" << k;
return QString();
#endif // BUNDLE_KEYBOARDLAYOUTS
}
/*! Helper function to add custom location of color schemes.
*/
namespace {
QStringList custom_color_schemes_dirs;
}
void add_custom_color_scheme_dir(const QString& custom_dir)
{
if (!custom_color_schemes_dirs.contains(custom_dir))
custom_color_schemes_dirs << custom_dir;
}
/*! Helper function to get possible locations of color schemes.
By default the COLORSCHEMES_DIR is used (linux/BSD/macports).
But in some cases (apple bundle) there can be more locations).
*/
const QStringList get_color_schemes_dirs()
{
#ifdef BUNDLE_COLORSCHEMES
return QLatin1String(":/");
#else
// qDebug() << __FILE__ << __FUNCTION__;
QStringList rval;
QString k(COLORSCHEMES_DIR);
QDir d(k);
// qDebug() << "default COLORSCHEMES_DIR: " << k;
if (d.exists())
rval << k.append("/");
// subdir in the app location
d.setPath(QCoreApplication::applicationDirPath() + "/color-schemes/");
//qDebug() << d.path();
if (d.exists())
{
if (!rval.isEmpty())
rval.clear();
rval << (QCoreApplication::applicationDirPath() + "/color-schemes/");
}
#ifdef Q_WS_MAC
d.setPath(QCoreApplication::applicationDirPath() + "/../Resources/color-schemes/");
if (d.exists())
{
if (!rval.isEmpty())
rval.clear();
rval << (QCoreApplication::applicationDirPath() + "/../Resources/color-schemes/");
}
#endif
for (const QString& custom_dir : const_cast<const QStringList&>(custom_color_schemes_dirs))
{
d.setPath(custom_dir);
if (d.exists())
rval << custom_dir;
}
#ifdef QT_DEBUG
if(!rval.isEmpty()) {
qDebug() << "Using color-schemes: " << rval;
} else {
qDebug() << "Cannot find color-schemes in any location!";
}
#endif
return rval;
#endif // BUNDLE_COLORSCHEMES
}