| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
| 3 | |
| 4 | #include "driver.h" |
| 5 | #include "uic.h" |
| 6 | #include "ui4.h" |
| 7 | |
| 8 | #include <language.h> |
| 9 | |
| 10 | #include <qfileinfo.h> |
| 11 | #include <qdebug.h> |
| 12 | |
| 13 | #include <algorithm> |
| 14 | |
| 15 | QT_BEGIN_NAMESPACE |
| 16 | |
| 17 | using namespace Qt::StringLiterals; |
| 18 | |
| 19 | Driver::Driver() |
| 20 | : m_stdout(stdout, QFile::WriteOnly | QFile::Text), |
| 21 | m_output(&m_stdout) |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | Driver::~Driver() = default; |
| 26 | |
| 27 | static inline QString spacerItemClass() { return QStringLiteral("QSpacerItem" ); } |
| 28 | static inline QString actionGroupClass() { return QStringLiteral("QActionGroup" ); } |
| 29 | static inline QString actionClass() { return QStringLiteral("QAction" ); } |
| 30 | static inline QString buttonGroupClass() { return QStringLiteral("QButtonGroup" ); } |
| 31 | |
| 32 | template <class DomClass> |
| 33 | Driver::DomObjectHashConstIt<DomClass> |
| 34 | Driver::findByAttributeNameIt(const DomObjectHash<DomClass> &domHash, |
| 35 | const QString &name) const |
| 36 | { |
| 37 | const auto end = domHash.cend(); |
| 38 | for (auto it = domHash.cbegin(); it != end; ++it) { |
| 39 | if (it.key()->attributeName() == name) |
| 40 | return it; |
| 41 | } |
| 42 | return end; |
| 43 | } |
| 44 | |
| 45 | template <class DomClass> |
| 46 | const DomClass *Driver::findByAttributeName(const DomObjectHash<DomClass> &domHash, |
| 47 | const QString &name) const |
| 48 | { |
| 49 | auto it = findByAttributeNameIt(domHash, name); |
| 50 | return it != domHash.cend() ? it.key() : nullptr; |
| 51 | } |
| 52 | |
| 53 | template <class DomClass> |
| 54 | QString Driver::findOrInsert(DomObjectHash<DomClass> *domHash, const DomClass *dom, |
| 55 | const QString &className, bool isMember) |
| 56 | { |
| 57 | auto it = domHash->find(dom); |
| 58 | if (it == domHash->end()) { |
| 59 | const QString name = this->unique(instanceName: dom->attributeName(), className); |
| 60 | it = domHash->insert(dom, isMember ? language::self + name : name); |
| 61 | } |
| 62 | return it.value(); |
| 63 | } |
| 64 | |
| 65 | QString Driver::findOrInsertWidget(const DomWidget *ui_widget) |
| 66 | { |
| 67 | // Top level is passed into setupUI(), everything else is a member variable |
| 68 | const bool isMember = !m_widgets.isEmpty(); |
| 69 | return findOrInsert(domHash: &m_widgets, dom: ui_widget, className: ui_widget->attributeClass(), isMember); |
| 70 | } |
| 71 | |
| 72 | QString Driver::findOrInsertSpacer(const DomSpacer *ui_spacer) |
| 73 | { |
| 74 | return findOrInsert(domHash: &m_spacers, dom: ui_spacer, className: spacerItemClass()); |
| 75 | } |
| 76 | |
| 77 | QString Driver::findOrInsertLayout(const DomLayout *ui_layout) |
| 78 | { |
| 79 | return findOrInsert(domHash: &m_layouts, dom: ui_layout, className: ui_layout->attributeClass()); |
| 80 | } |
| 81 | |
| 82 | QString Driver::findOrInsertLayoutItem(const DomLayoutItem *ui_layoutItem) |
| 83 | { |
| 84 | switch (ui_layoutItem->kind()) { |
| 85 | case DomLayoutItem::Widget: |
| 86 | return findOrInsertWidget(ui_widget: ui_layoutItem->elementWidget()); |
| 87 | case DomLayoutItem::Spacer: |
| 88 | return findOrInsertSpacer(ui_spacer: ui_layoutItem->elementSpacer()); |
| 89 | case DomLayoutItem::Layout: |
| 90 | return findOrInsertLayout(ui_layout: ui_layoutItem->elementLayout()); |
| 91 | case DomLayoutItem::Unknown: |
| 92 | break; |
| 93 | } |
| 94 | |
| 95 | Q_ASSERT( 0 ); |
| 96 | |
| 97 | return {}; |
| 98 | } |
| 99 | |
| 100 | QString Driver::findOrInsertActionGroup(const DomActionGroup *ui_group) |
| 101 | { |
| 102 | return findOrInsert(domHash: &m_actionGroups, dom: ui_group, className: actionGroupClass()); |
| 103 | } |
| 104 | |
| 105 | QString Driver::findOrInsertAction(const DomAction *ui_action) |
| 106 | { |
| 107 | return findOrInsert(domHash: &m_actions, dom: ui_action, className: actionClass()); |
| 108 | } |
| 109 | |
| 110 | QString Driver::findOrInsertButtonGroup(const DomButtonGroup *ui_group) |
| 111 | { |
| 112 | return findOrInsert(domHash: &m_buttonGroups, dom: ui_group, className: buttonGroupClass()); |
| 113 | } |
| 114 | |
| 115 | // Find a group by its non-uniqified name |
| 116 | const DomButtonGroup *Driver::findButtonGroup(const QString &attributeName) const |
| 117 | { |
| 118 | return findByAttributeName(domHash: m_buttonGroups, name: attributeName); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | QString Driver::findOrInsertName(const QString &name) |
| 123 | { |
| 124 | return unique(instanceName: name); |
| 125 | } |
| 126 | |
| 127 | QString Driver::normalizedName(const QString &name) |
| 128 | { |
| 129 | QString result = name; |
| 130 | std::replace_if(first: result.begin(), last: result.end(), |
| 131 | pred: [] (QChar c) { return !c.isLetterOrNumber(); }, |
| 132 | new_value: u'_'); |
| 133 | return result; |
| 134 | } |
| 135 | |
| 136 | QString Driver::unique(const QString &instanceName, const QString &className) |
| 137 | { |
| 138 | QString name; |
| 139 | bool alreadyUsed = false; |
| 140 | |
| 141 | if (!instanceName.isEmpty()) { |
| 142 | name = normalizedName(name: instanceName); |
| 143 | QString base = name; |
| 144 | |
| 145 | for (int id = 1; m_nameRepository.contains(key: name); ++id) { |
| 146 | alreadyUsed = true; |
| 147 | name = base + QString::number(id); |
| 148 | } |
| 149 | } else if (!className.isEmpty()) { |
| 150 | name = unique(instanceName: qtify(name: className)); |
| 151 | } else { |
| 152 | name = unique(instanceName: "var"_L1 ); |
| 153 | } |
| 154 | |
| 155 | if (alreadyUsed && !className.isEmpty()) { |
| 156 | fprintf(stderr, format: "%s: Warning: The name '%s' (%s) is already in use, defaulting to '%s'.\n" , |
| 157 | qPrintable(m_option.messagePrefix()), |
| 158 | qPrintable(instanceName), qPrintable(className), |
| 159 | qPrintable(name)); |
| 160 | } |
| 161 | |
| 162 | m_nameRepository.insert(key: name, value: true); |
| 163 | return name; |
| 164 | } |
| 165 | |
| 166 | QString Driver::qtify(const QString &name) |
| 167 | { |
| 168 | QString qname = name; |
| 169 | |
| 170 | if (qname.at(i: 0) == u'Q' || qname.at(i: 0) == u'K') |
| 171 | qname.remove(i: 0, len: 1); |
| 172 | |
| 173 | for (qsizetype i = 0, size = qname.size(); i < size && qname.at(i).isUpper(); ++i) |
| 174 | qname[i] = qname.at(i).toLower(); |
| 175 | |
| 176 | return qname; |
| 177 | } |
| 178 | |
| 179 | static bool isAnsiCCharacter(QChar c) |
| 180 | { |
| 181 | return (c.toUpper() >= u'A' && c.toUpper() <= u'Z') || c.isDigit() || c == u'_'; |
| 182 | } |
| 183 | |
| 184 | QString Driver::() const |
| 185 | { |
| 186 | QString name = m_option.outputFile; |
| 187 | |
| 188 | if (name.isEmpty()) { |
| 189 | name = "ui_"_L1 ; // ### use ui_ as prefix. |
| 190 | name.append(s: m_option.inputFile); |
| 191 | } |
| 192 | |
| 193 | return headerFileName(fileName: name); |
| 194 | } |
| 195 | |
| 196 | QString Driver::(const QString &fileName) |
| 197 | { |
| 198 | if (fileName.isEmpty()) |
| 199 | return headerFileName(fileName: u"noname"_s ); |
| 200 | |
| 201 | QFileInfo info(fileName); |
| 202 | QString baseName = info.baseName(); |
| 203 | // Transform into a valid C++ identifier |
| 204 | if (!baseName.isEmpty() && baseName.at(i: 0).isDigit()) |
| 205 | baseName.prepend(c: u'_'); |
| 206 | for (qsizetype i = 0; i < baseName.size(); ++i) { |
| 207 | QChar c = baseName.at(i); |
| 208 | if (!isAnsiCCharacter(c)) { |
| 209 | // Replace character by its unicode value |
| 210 | QString hex = QString::number(c.unicode(), base: 16); |
| 211 | baseName.replace(i, len: 1, after: u'_' + hex + u'_'); |
| 212 | i += hex.size() + 1; |
| 213 | } |
| 214 | } |
| 215 | return baseName.toUpper() + "_H"_L1 ; |
| 216 | } |
| 217 | |
| 218 | bool Driver::printDependencies(const QString &fileName) |
| 219 | { |
| 220 | Q_ASSERT(m_option.dependencies == true); |
| 221 | |
| 222 | m_option.inputFile = fileName; |
| 223 | |
| 224 | Uic tool(this); |
| 225 | return tool.printDependencies(); |
| 226 | } |
| 227 | |
| 228 | bool Driver::uic(const QString &fileName, DomUI *ui, QTextStream *out) |
| 229 | { |
| 230 | m_option.inputFile = fileName; |
| 231 | setUseIdBasedTranslations(ui->attributeIdbasedtr()); |
| 232 | |
| 233 | QTextStream *oldOutput = m_output; |
| 234 | |
| 235 | m_output = out != nullptr ? out : &m_stdout; |
| 236 | |
| 237 | Uic tool(this); |
| 238 | const bool result = tool.write(ui); |
| 239 | |
| 240 | m_output = oldOutput; |
| 241 | |
| 242 | return result; |
| 243 | } |
| 244 | |
| 245 | bool Driver::uic(const QString &fileName, QTextStream *out) |
| 246 | { |
| 247 | QFile f; |
| 248 | if (fileName.isEmpty()) { |
| 249 | if (!f.open(stdin, ioFlags: QIODevice::ReadOnly)) |
| 250 | return false; |
| 251 | } else { |
| 252 | f.setFileName(fileName); |
| 253 | if (!f.open(flags: QIODevice::ReadOnly)) |
| 254 | return false; |
| 255 | } |
| 256 | |
| 257 | m_option.inputFile = fileName; |
| 258 | |
| 259 | QTextStream *oldOutput = m_output; |
| 260 | bool deleteOutput = false; |
| 261 | |
| 262 | if (out) { |
| 263 | m_output = out; |
| 264 | } else { |
| 265 | #ifdef Q_OS_WIN |
| 266 | // As one might also redirect the output to a file on win, |
| 267 | // we should not create the textstream with QFile::Text flag. |
| 268 | // The redirected file is opened in TextMode and this will |
| 269 | // result in broken line endings as writing will replace \n again. |
| 270 | m_output = new QTextStream(stdout, QIODevice::WriteOnly); |
| 271 | #else |
| 272 | m_output = new QTextStream(stdout, QIODevice::WriteOnly | QFile::Text); |
| 273 | #endif |
| 274 | deleteOutput = true; |
| 275 | } |
| 276 | |
| 277 | Uic tool(this); |
| 278 | bool rtn = tool.write(in: &f); |
| 279 | f.close(); |
| 280 | |
| 281 | if (deleteOutput) |
| 282 | delete m_output; |
| 283 | |
| 284 | m_output = oldOutput; |
| 285 | |
| 286 | return rtn; |
| 287 | } |
| 288 | |
| 289 | const DomWidget *Driver::widgetByName(const QString &attributeName) const |
| 290 | { |
| 291 | return findByAttributeName(domHash: m_widgets, name: attributeName); |
| 292 | } |
| 293 | |
| 294 | QString Driver::widgetVariableName(const QString &attributeName) const |
| 295 | { |
| 296 | auto it = findByAttributeNameIt(domHash: m_widgets, name: attributeName); |
| 297 | return it != m_widgets.cend() ? it.value() : QString(); |
| 298 | } |
| 299 | |
| 300 | const DomActionGroup *Driver::actionGroupByName(const QString &attributeName) const |
| 301 | { |
| 302 | return findByAttributeName(domHash: m_actionGroups, name: attributeName); |
| 303 | } |
| 304 | |
| 305 | const DomAction *Driver::actionByName(const QString &attributeName) const |
| 306 | { |
| 307 | return findByAttributeName(domHash: m_actions, name: attributeName); |
| 308 | } |
| 309 | |
| 310 | QT_END_NAMESPACE |
| 311 | |