BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
widgetutils.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/view/mvvm/widgets/widgetutils.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
17 #include <QApplication>
18 #include <QColor>
19 #include <QDir>
20 #include <QFontMetrics>
21 #include <QLabel>
22 #include <QMainWindow>
23 #include <QSize>
24 
25 namespace {
26 
27 //! Calculates size of letter `M` for current system font settings.
28 
29 QSize FindSizeOfLetterM()
30 {
31  QFontMetrics fontMetric(QApplication::font());
32  auto em = fontMetric.horizontalAdvance('M');
33  auto fontAscent = fontMetric.ascent();
34  return QSize(em, fontAscent);
35 }
36 
37 const QString untitled_name = "Untitled";
38 
39 } // namespace
40 
42 {
43  auto rndm = []() -> int { return ModelView::Utils::RandInt(0, 255); };
44  return QColor(rndm(), rndm(), rndm());
45 }
46 
48 {
49  return RandomColor().name().toStdString();
50 }
51 
53 {
54 #if defined(Q_OS_WIN)
55  return true;
56 #else
57  return false;
58 #endif
59 }
60 
62 {
63 #if defined(Q_OS_MAC)
64  return true;
65 #else
66  return false;
67 #endif
68 }
69 
71 {
72 #if defined(Q_OS_LINUX)
73  return true;
74 #else
75  return false;
76 #endif
77 }
78 
79 QString ModelView::Utils::WithTildeHomePath(const QString& path)
80 {
82  return path;
83 
84  static const QString homePath = QDir::homePath();
85 
86  QFileInfo fi(QDir::cleanPath(path));
87  QString outPath = fi.absoluteFilePath();
88  if (outPath.startsWith(homePath))
89  outPath = QLatin1Char('~') + outPath.mid(homePath.size());
90  else
91  outPath = path;
92  return outPath;
93 }
94 
95 //! Project without projectDir will be "Untitled", modified project will be "*Untitled".
96 //! Project with projectDir in "/home/user/project1" will get title "project1".
97 
98 QString ModelView::Utils::ProjectWindowTitle(const QString& project_dir, bool is_modified)
99 {
100  auto pos = project_dir.lastIndexOf('/');
101  auto project_name = (pos == -1) ? untitled_name : project_dir.mid(pos + 1);
102  auto unsaved_status = is_modified ? QString("*") : QString();
103  return unsaved_status + project_name;
104 }
105 
107 {
108  return ModelView::Utils::SizeOfLetterM().width();
109 }
110 
112 {
113  return ModelView::Utils::SizeOfLetterM().height();
114 }
115 
117 {
118  static QSize result = FindSizeOfLetterM();
119  return result;
120 }
121 
123 {
124  return QApplication::font().pointSize();
125 }
126 
128 {
129  for (auto widget : qApp->topLevelWidgets()) {
130  if (auto result = dynamic_cast<QMainWindow*>(widget); result)
131  return result;
132  }
133  return nullptr;
134 }
135 
136 QString ModelView::Utils::ClickableText(const QString& text, const QString& tag)
137 {
138  return QString("<a href=\"%1\">%2</a>").arg(tag.isEmpty() ? text : tag, text);
139 }
140 
141 void ModelView::Utils::ScaleLabelFont(QLabel* label, double scale)
142 {
143  QFont font = label->font();
144  font.setPointSize(ModelView::Utils::SystemPointSize() * scale);
145  label->setFont(font);
146 }
147 
148 QStringList ModelView::Utils::toStringList(const std::vector<std::string>& vec)
149 {
150  QStringList result;
151  for (const auto& x : vec)
152  result.push_back(QString::fromStdString(x));
153  return result;
154 }
155 
156 std::vector<std::string> ModelView::Utils::fromStringList(const QStringList& string_list)
157 {
158  std::vector<std::string> result;
159  for (const auto& x : string_list)
160  result.push_back(x.toStdString());
161  return result;
162 }
163 
164 QByteArray ModelView::Utils::serialize(const QStringList& data)
165 {
166  QByteArray byteArray;
167  QDataStream out(&byteArray, QIODevice::WriteOnly);
168  out << data;
169  return byteArray;
170 }
171 
172 QStringList ModelView::Utils::deserialize(const QByteArray& byteArray)
173 {
174  QByteArray array = byteArray;
175  QStringList result;
176  QDataStream in(&array, QIODevice::ReadOnly);
177  in >> result;
178  return result;
179 }
MVVM_VIEW_EXPORT std::string RandomNamedColor()
Returns the name of random color.
Definition: widgetutils.cpp:47
MVVM_VIEW_EXPORT int HeightOfLetterM()
Returns height of the letter 'M' deduced from current font metrics.
MVVM_VIEW_EXPORT QStringList deserialize(const QByteArray &byteArray)
Converts byte array to vector of strings.
MVVM_VIEW_EXPORT QString WithTildeHomePath(const QString &path)
Returns a string where Linux path to the file is striped using '~/'.
Definition: widgetutils.cpp:79
MVVM_VIEW_EXPORT bool IsWindowsHost()
Returns true if it is Windows.
Definition: widgetutils.cpp:52
MVVM_VIEW_EXPORT QSize SizeOfLetterM()
Returns size corresponding to actual size of letter M basing on current font metrics.
MVVM_VIEW_EXPORT bool IsMacHost()
Returns true if it is Mac.
Definition: widgetutils.cpp:61
MVVM_VIEW_EXPORT QByteArray serialize(const QStringList &data)
Converts vector of strings to byte array.
MVVM_VIEW_EXPORT QStringList toStringList(const std::vector< std::string > &vec)
Converts vector of strings to QStringList.
MVVM_VIEW_EXPORT QString ClickableText(const QString &text, const QString &tag={})
Returns text wrapped into 'href' tag to provide clickable links in QLabel.
MVVM_VIEW_EXPORT void ScaleLabelFont(QLabel *label, double scale)
Set label's font size to system font size scaled by given factor.
MVVM_VIEW_EXPORT QMainWindow * FindMainWindow()
Finds main window.
MVVM_VIEW_EXPORT QColor RandomColor()
Returns random color.
Definition: widgetutils.cpp:41
MVVM_VIEW_EXPORT std::vector< std::string > fromStringList(const QStringList &string_list)
Converts vector of strings to QStringList.
MVVM_VIEW_EXPORT int WidthOfLetterM()
Returns width of the letter 'M' deduced from current font metrics.
MVVM_VIEW_EXPORT bool IsLinuxHost()
Returns true if it is Linux.
Definition: widgetutils.cpp:70
MVVM_VIEW_EXPORT int SystemPointSize()
Returns size in points of default system font.
MVVM_MODEL_EXPORT int RandInt(int low, int high)
Produces random integer values uniformly distributed on the closed interval [low, high].
MVVM_VIEW_EXPORT QString ProjectWindowTitle(const QString &project_dir, bool is_modified)
Returns a title composed from last part of project path, and is_modified flag.
Definition: widgetutils.cpp:98
Defines class CLASS?
Defines class CLASS?