1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
/*
* Copyright 2012, 2013 Thomas Schöps
* Copyright 2013-2015 Kai Pastor
*
* This file is part of OpenOrienteering.
*
* OpenOrienteering 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 3 of the License, or
* (at your option) any later version.
*
* OpenOrienteering 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.
*
* You should have received a copy of the GNU General Public License
* along with OpenOrienteering. If not, see <http://www.gnu.org/licenses/>.
*/
#include "util.h"
#include <QApplication>
#include <QDebug>
#include <QDir>
#include <QIODevice>
#include <QMessageBox>
#include <QProcess>
#include <QSettings>
#include <QScreen>
#include <QUrl>
#include "core/map_coord.h"
#include "mapper_resource.h"
#include "settings.h"
#include "gui/text_browser_dialog.h"
#include <mapper_config.h>
DoubleValidator::DoubleValidator(double bottom, double top, QObject* parent, int decimals) : QDoubleValidator(bottom, top, decimals, parent)
{
// Don't cause confusion, accept only English formatting
setLocale(QLocale(QLocale::English));
}
QValidator::State DoubleValidator::validate(QString& input, int& pos) const
{
// Transform thousands group separators into decimal points to avoid confusion
input.replace(QLatin1Char(','), QLatin1Char('.'));
return QDoubleValidator::validate(input, pos);
}
void blockSignalsRecursively(QObject* obj, bool block)
{
if (!obj)
return;
obj->blockSignals(block);
const QObjectList& list = obj->children();
for (QObjectList::const_iterator it = list.begin(), end = list.end(); it != end; ++it)
blockSignalsRecursively(*it, block);
}
void rectInclude(QRectF& rect, QPointF point)
{
if (point.x() < rect.left())
rect.setLeft(point.x());
else if (point.x() > rect.right())
rect.setRight(point.x());
if (point.y() < rect.top())
rect.setTop(point.y());
else if (point.y() > rect.bottom())
rect.setBottom(point.y());
}
void rectIncludeSafe(QRectF& rect, QPointF point)
{
if (rect.isValid())
rectInclude(rect, point);
else
rect = QRectF(point.x(), point.y(), 0.0001, 0.0001);
}
void rectInclude(QRectF& rect, const QRectF& other_rect)
{
if (other_rect.left() < rect.left())
rect.setLeft(other_rect.left());
if (other_rect.right() > rect.right())
rect.setRight(other_rect.right());
if (other_rect.top() < rect.top())
rect.setTop(other_rect.top());
if (other_rect.bottom() > rect.bottom())
rect.setBottom(other_rect.bottom());
}
void rectIncludeSafe(QRectF& rect, const QRectF& other_rect)
{
if (other_rect.isValid())
{
if (rect.isValid())
rectInclude(rect, other_rect);
else
rect = other_rect;
}
}
void rectIncludeSafe(QRect& rect, const QRect& other_rect)
{
if (other_rect.isValid())
{
if (rect.isValid())
{
if (other_rect.left() < rect.left())
rect.setLeft(other_rect.left());
if (other_rect.right() > rect.right())
rect.setRight(other_rect.right());
if (other_rect.top() < rect.top())
rect.setTop(other_rect.top());
if (other_rect.bottom() > rect.bottom())
rect.setBottom(other_rect.bottom());
}
else
rect = other_rect;
}
}
bool lineIntersectsRect(const QRectF& rect, const QPointF& p1, const QPointF& p2)
{
if (rect.contains(p1) || rect.contains(p2))
return true;
// Left side
if ((p1.x() > rect.left()) != (p2.x() > rect.left()))
{
if ((p2.x() == p1.x()) && !((p1.y() < rect.top() && p2.y() < rect.top()) || (p1.y() > rect.bottom() && p2.y() > rect.bottom())))
return true;
qreal intersection_y = p1.y() + (p2.y() - p1.y()) * (rect.left() - p1.x()) / (p2.x() - p1.x());
if (intersection_y >= rect.top() && intersection_y <= rect.bottom())
return true;
}
// Right side
if ((p1.x() > rect.right()) != (p2.x() > rect.right()))
{
if ((p2.x() == p1.x()) && !((p1.y() < rect.top() && p2.y() < rect.top()) || (p1.y() > rect.bottom() && p2.y() > rect.bottom())))
return true;
qreal intersection_y = p1.y() + (p2.y() - p1.y()) * (rect.right() - p1.x()) / (p2.x() - p1.x());
if (intersection_y >= rect.top() && intersection_y <= rect.bottom())
return true;
}
// Top side
if ((p1.y() > rect.top()) != (p2.y() > rect.top()))
{
if ((p2.y() == p1.y()) && !((p1.x() < rect.left() && p2.x() < rect.left()) || (p1.x() > rect.right() && p2.x() > rect.right())))
return true;
qreal intersection_x = p1.x() + (p2.x() - p1.x()) * (rect.top() - p1.y()) / (p2.y() - p1.y());
if (intersection_x >= rect.left() && intersection_x <= rect.right())
return true;
}
// Bottom side
if ((p1.y() > rect.bottom()) != (p2.y() > rect.bottom()))
{
if ((p2.y() == p1.y()) && !((p1.x() < rect.left() && p2.x() < rect.left()) || (p1.x() > rect.right() && p2.x() > rect.right())))
return true;
qreal intersection_x = p1.x() + (p2.x() - p1.x()) * (rect.bottom() - p1.y()) / (p2.y() - p1.y());
if (intersection_x >= rect.left() && intersection_x <= rect.right())
return true;
}
return false;
}
double parameterOfPointOnLine(double x0, double y0, double dx, double dy, double x, double y, bool& ok)
{
const double epsilon = 1e-5;
ok = true;
if (qAbs(dx) > qAbs(dy))
{
double param = (x - x0) / dx;
if (qAbs(y0 + param * dy - y) < epsilon)
return param;
}
else if (dy != 0)
{
double param = (y - y0) / dy;
if (qAbs(x0 + param * dx - x) < epsilon)
return param;
}
ok = false;
return -1;
}
bool isPointOnSegment(const MapCoordF& seg_start, const MapCoordF& seg_end, const MapCoordF& point)
{
bool ok;
double param = parameterOfPointOnLine(seg_start.x(), seg_start.y(),
seg_end.x() - seg_start.x(), seg_end.y() - seg_start.y(),
point.x(), point.y(), ok);
return ok && param >= 0 && param <= 1;
}
void loadString(QIODevice* file, QString& str)
{
int length;
file->read((char*)&length, sizeof(int));
if (length > 0)
{
str.resize(length);
file->read((char*)str.data(), length * sizeof(QChar));
}
else
str.clear();
}
namespace Util
{
void showHelp(QWidget* dialog_parent, const char* filename_latin1, const char* anchor_latin1)
{
showHelp(dialog_parent, QString::fromLatin1(filename_latin1) + QLatin1Char('#') + QString::fromLatin1(anchor_latin1));
}
void showHelp(QWidget* dialog_parent, const char* file_and_anchor_latin1)
{
showHelp(dialog_parent, QString::fromLatin1(file_and_anchor_latin1));
}
void showHelp(QWidget* dialog_parent, QString filename)
{
#if defined(Q_OS_ANDROID)
const QString manual_path = MapperResource::locate(MapperResource::MANUAL, filename);
const QUrl help_url = QUrl::fromLocalFile(manual_path);
TextBrowserDialog help_dialog(help_url, dialog_parent);
help_dialog.exec();
return;
#else
const auto base_url = QLatin1String("qthelp://" MAPPER_HELP_NAMESPACE "/manual/");
static QProcess assistant_process;
if (assistant_process.state() == QProcess::Running)
{
QString command = QLatin1String("setSource ") + base_url + filename + QLatin1Char('\n');
assistant_process.write(command.toLatin1());
}
else
{
const QString help_collection_path(MapperResource::locate(MapperResource::MANUAL, QString::fromUtf8("Mapper " APP_VERSION " Manual.qhc")));
const QString compiled_help_path(MapperResource::locate(MapperResource::MANUAL, QString::fromUtf8("Mapper " APP_VERSION " Manual.qch")));
if (help_collection_path.isEmpty() || compiled_help_path.isEmpty())
{
QMessageBox::warning(dialog_parent, QApplication::translate("Util", "Error"), QApplication::translate("Util", "Failed to locate the help files."));
return;
}
QString assistant_path = MapperResource::locate(MapperResource::ASSISTANT);
if (assistant_path.isEmpty())
{
QMessageBox::warning(dialog_parent, QApplication::translate("Util", "Error"), QApplication::translate("Util", "Failed to locate the help browser (\"Qt Assistant\")."));
return;
}
// Try to start the Qt Assistant process
QStringList args;
args << QLatin1String("-collectionFile")
<< QDir::toNativeSeparators(help_collection_path)
<< QLatin1String("-showUrl")
<< (base_url + filename)
<< QLatin1String("-enableRemoteControl");
if ( QGuiApplication::platformName() == QLatin1String("xcb") ||
QGuiApplication::platformName().isEmpty() )
{
// Use the modern 'fusion' style instead of the default "windows"
// style on X11.
args << QLatin1String("-style") << QLatin1String("fusion");
}
#if defined(Q_OS_LINUX)
auto env = QProcessEnvironment::systemEnvironment();
env.insert(QLatin1String("QT_SELECT"), QLatin1String("5")); // #541
assistant_process.setProcessEnvironment(env);
#endif
assistant_process.start(assistant_path, args);
// FIXME: Calling waitForStarted() from the main thread might cause the user interface to freeze.
if (!assistant_process.waitForStarted())
{
QMessageBox msg_box;
msg_box.setIcon(QMessageBox::Warning);
msg_box.setWindowTitle(QApplication::translate("Util", "Error"));
msg_box.setText(QApplication::translate("Util", "Failed to launch the help browser (\"Qt Assistant\")."));
msg_box.setStandardButtons(QMessageBox::Ok);
auto details = assistant_process.readAllStandardError();
if (! details.isEmpty())
msg_box.setDetailedText(QString::fromLocal8Bit(details));
msg_box.exec();
}
}
#endif
}
QString makeWhatThis(const char* reference_latin1)
{
//: This "See more" is displayed as a link to the manual in What's-this tooltips.
return QStringLiteral("<a href=\"%1\">%2</a>").arg(
QString::fromLatin1(reference_latin1), QApplication::translate("Util", "See more...") );
}
qreal mmToPixelPhysical(qreal millimeters)
{
auto ppi = Settings::getInstance().getSettingCached(Settings::General_PixelsPerInch).toReal();
return millimeters * ppi / 25.4;
}
qreal pixelToMMPhysical(qreal pixels)
{
auto ppi = Settings::getInstance().getSettingCached(Settings::General_PixelsPerInch).toReal();
return pixels * 25.4 / ppi;
}
qreal mmToPixelLogical(qreal millimeters)
{
auto ppi = QApplication::primaryScreen()->logicalDotsPerInch();
return millimeters * ppi / 25.4f;
}
qreal pixelToMMLogical(qreal pixels)
{
auto ppi = QApplication::primaryScreen()->logicalDotsPerInch();
return pixels * 25.4f / ppi;
}
bool isAntialiasingRequired()
{
return isAntialiasingRequired(Settings::getInstance().getSettingCached(Settings::General_PixelsPerInch).toReal());
}
}
|