| 1 |
// Copyright (C) 2025 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include <QtCore> |
| 5 |
#include <QtWidgets> |
| 6 |
|
| 7 |
#if __has_include(<QtQml>) |
| 8 |
#define QUICK_UI |
| 9 |
#include <QtQml/QQmlApplicationEngine> |
| 10 |
#include <QtQml/QQmlContext> |
| 11 |
#include <QQuickWidget> |
| 12 |
#include <QQuickItem> |
| 13 |
#else |
| 14 |
#warning "Building without Quick UI" |
| 15 |
#endif |
| 16 |
|
| 17 |
#include <list> |
| 18 |
#include <ranges> |
| 19 |
#include <vector> |
| 20 |
|
| 21 |
using namespace Qt::StringLiterals; |
| 22 |
|
| 23 |
class Gadget |
| 24 |
{ |
| 25 |
Q_GADGET |
| 26 |
Q_PROPERTY(QString display READ display WRITE setDisplay) |
| 27 |
Q_PROPERTY(QColor decoration READ decoration WRITE setDecoration) |
| 28 |
Q_PROPERTY(QString toolTip READ toolTip WRITE setToolTip) |
| 29 |
public: |
| 30 |
Gadget() = default; |
| 31 |
|
| 32 |
Gadget(const QString &display, QColor decoration, const QString &toolTip) |
| 33 |
: m_display(display), m_decoration(decoration), m_toolTip(toolTip) |
| 34 |
{ |
| 35 |
} |
| 36 |
|
| 37 |
QString display() const { return m_display; } |
| 38 |
void setDisplay(const QString &display) { m_display = display; } |
| 39 |
QColor decoration() const { return m_decoration; } |
| 40 |
void setDecoration(QColor decoration) { m_decoration = decoration; } |
| 41 |
QString toolTip() const { return m_toolTip.isEmpty() ? m_display : m_toolTip; } |
| 42 |
void setToolTip(const QString &toolTip) { m_toolTip = toolTip; } |
| 43 |
|
| 44 |
private: |
| 45 |
QString m_display; |
| 46 |
QColor m_decoration; |
| 47 |
QString m_toolTip; |
| 48 |
}; |
| 49 |
|
| 50 |
struct QMetaEnumerator |
| 51 |
{ |
| 52 |
struct iterator |
| 53 |
{ |
| 54 |
using difference_type = int; |
| 55 |
using size_type = int; |
| 56 |
using pointer = void; |
| 57 |
using iterator_category = std::input_iterator_tag; |
| 58 |
using value_type = std::tuple<int, QByteArray, int>; |
| 59 |
using reference = value_type; |
| 60 |
using const_reference = const value_type; |
| 61 |
|
| 62 |
friend constexpr iterator &operator++(iterator &that) |
| 63 |
{ ++that.m_index; return that; } |
| 64 |
friend constexpr iterator operator++(iterator &that, int) |
| 65 |
{ auto copy = that; ++that.m_index; return copy; } |
| 66 |
friend constexpr iterator &operator+=(iterator &that, int n) |
| 67 |
{ that.m_index += n; return that; } |
| 68 |
|
| 69 |
friend constexpr bool comparesEqual(const iterator &lhs, const iterator &rhs) noexcept |
| 70 |
{ |
| 71 |
return lhs.m_index == rhs.m_index && lhs.m_enum == rhs.m_enum; |
| 72 |
} |
| 73 |
|
| 74 |
friend constexpr Qt::strong_ordering compareThreeWay(const iterator &lhs, |
| 75 |
const iterator &rhs) noexcept |
| 76 |
{ |
| 77 |
return Qt::compareThreeWay(lhs.m_index, rhs.m_index); |
| 78 |
} |
| 79 |
Q_DECLARE_STRONGLY_ORDERED(iterator) |
| 80 |
|
| 81 |
const_reference operator*() const |
| 82 |
{ return {m_index, m_enum->key(m_index), m_enum->value(m_index)}; } |
| 83 |
|
| 84 |
const QMetaEnum *m_enum = nullptr; |
| 85 |
int m_index = 0; |
| 86 |
}; |
| 87 |
|
| 88 |
static_assert(std::input_iterator<iterator>); |
| 89 |
|
| 90 |
using size_type = iterator::size_type; |
| 91 |
using value_type = iterator::value_type; |
| 92 |
using const_iterator = iterator; |
| 93 |
|
| 94 |
template <typename Enum> |
| 95 |
explicit QMetaEnumerator(Enum e) noexcept |
| 96 |
: m_enum(QMetaEnum::fromType<Enum>()) |
| 97 |
{} |
| 98 |
|
| 99 |
const_iterator begin() const { return iterator{&m_enum, 0}; } |
| 100 |
const_iterator end() const { return iterator{&m_enum, size()}; } |
| 101 |
size_type size() const { return m_enum.keyCount(); } |
| 102 |
|
| 103 |
private: |
| 104 |
const QMetaEnum m_enum; |
| 105 |
}; |
| 106 |
|
| 107 |
struct TreeRow; |
| 108 |
using Tree = std::vector<TreeRow>; |
| 109 |
|
| 110 |
struct TreeRow |
| 111 |
{ |
| 112 |
public: |
| 113 |
TreeRow() = default; |
| 114 |
|
| 115 |
TreeRow(const QString &name, const QString &title) |
| 116 |
: m_name(name), m_title(title) |
| 117 |
{} |
| 118 |
|
| 119 |
~TreeRow() = default; |
| 120 |
TreeRow(TreeRow &&other) = default; |
| 121 |
TreeRow &operator=(TreeRow &&other) = default; |
| 122 |
|
| 123 |
template <typename ...Args> |
| 124 |
TreeRow &addChild(Args&& ...args) |
| 125 |
{ |
| 126 |
if (!m_children) |
| 127 |
m_children.emplace(Tree{}); |
| 128 |
TreeRow &res = m_children->emplace_back(args...); |
| 129 |
res.m_parent = this; |
| 130 |
return res; |
| 131 |
} |
| 132 |
|
| 133 |
// tree traversal protocol implementation |
| 134 |
TreeRow *parentRow() const { return m_parent; } |
| 135 |
void setParentRow(TreeRow *parent) { m_parent = parent; } |
| 136 |
const std::optional<Tree> &childRows() const { return m_children; } |
| 137 |
std::optional<Tree> &childRows() { return m_children; } |
| 138 |
|
| 139 |
private: |
| 140 |
QString m_name; |
| 141 |
QString m_title; |
| 142 |
|
| 143 |
TreeRow *m_parent = nullptr; |
| 144 |
std::optional<Tree> m_children = std::nullopt; |
| 145 |
|
| 146 |
template<size_t I, typename Row, |
| 147 |
std::enable_if_t<std::is_same_v<q20::remove_cvref_t<Row>, TreeRow>, bool> = true> |
| 148 |
friend inline decltype(auto) get(Row &&row) |
| 149 |
{ |
| 150 |
if constexpr (I == 0) |
| 151 |
return q23::forward_like<Row>(row.m_name); |
| 152 |
else if constexpr (I == 1) |
| 153 |
return q23::forward_like<Row>(row.m_title); |
| 154 |
} |
| 155 |
}; |
| 156 |
|
| 157 |
namespace std { |
| 158 |
template <> struct tuple_size<TreeRow> : std::integral_constant<size_t, 2> {}; |
| 159 |
template <size_t I> struct tuple_element<I, TreeRow> |
| 160 |
{ using type = decltype(get<I>(std::declval<TreeRow>())); }; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
class Object : public QObject |
| 165 |
{ |
| 166 |
Q_OBJECT |
| 167 |
Q_PROPERTY(QString display READ display WRITE setDisplay NOTIFY displayChanged) |
| 168 |
|
| 169 |
public: |
| 170 |
Object(int x) |
| 171 |
: m_display(QString::number(x)) |
| 172 |
{} |
| 173 |
QString display() const { return m_display; } |
| 174 |
void setDisplay(const QString &d) { m_display = d; displayChanged(); } |
| 175 |
|
| 176 |
Q_SIGNALS: |
| 177 |
void displayChanged(); |
| 178 |
|
| 179 |
private: |
| 180 |
QString m_display; |
| 181 |
}; |
| 182 |
|
| 183 |
template <> |
| 184 |
struct QRangeModel::RowOptions<Object> |
| 185 |
{ |
| 186 |
static constexpr auto rowCategory = QRangeModel::RowCategory::MultiRoleItem; |
| 187 |
}; |
| 188 |
|
| 189 |
class ModelFactory : public QObject |
| 190 |
{ |
| 191 |
Q_OBJECT |
| 192 |
|
| 193 |
std::vector<int> numbers = {1, 2, 3, 4, 5}; |
| 194 |
QList<QString> strings = {u"one"_s, u"two"_s, u"three"_s}; |
| 195 |
std::array<int, 1000000> largeArray = {}; |
| 196 |
|
| 197 |
public slots: |
| 198 |
QRangeModel *makeNumbers() |
| 199 |
{ |
| 200 |
return new QRangeModel(&numbers); |
| 201 |
} |
| 202 |
|
| 203 |
QRangeModel *makeLargeArray() |
| 204 |
{ |
| 205 |
return new QRangeModel(&largeArray); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
QRangeModel *makeStrings() |
| 210 |
{ |
| 211 |
return new QRangeModel(std::ref(strings)); |
| 212 |
} |
| 213 |
|
| 214 |
QRangeModel *makeJson() |
| 215 |
{ |
| 216 |
QJsonDocument json = QJsonDocument::fromJson(R"( |
| 217 |
[ "one", "two", 12345 ] |
| 218 |
)"); |
| 219 |
Q_ASSERT(json.isArray()); |
| 220 |
return new QRangeModel(json.array()); |
| 221 |
} |
| 222 |
|
| 223 |
QRangeModel *makeListOfTuples() |
| 224 |
{ |
| 225 |
std::list<std::tuple<int, QString>> data = { |
| 226 |
{ 1, "eins"}, |
| 227 |
{ 2, "zwei"}, |
| 228 |
{ 3, "drei"}, |
| 229 |
{ 4, "vier"}, |
| 230 |
{ 5, "fünf"}, |
| 231 |
}; |
| 232 |
return new QRangeModel(data); |
| 233 |
} |
| 234 |
|
| 235 |
QRangeModel *makeListOfArrays() |
| 236 |
{ |
| 237 |
QList<std::array<double, 2000>> data = { |
| 238 |
{0.0}, |
| 239 |
{1.1}, |
| 240 |
{2.2}, |
| 241 |
}; |
| 242 |
|
| 243 |
return new QRangeModel(std::move(data)); |
| 244 |
} |
| 245 |
|
| 246 |
QRangeModel *makeCustomFromEnum() |
| 247 |
{ |
| 248 |
return new QRangeModel(QMetaEnumerator(Qt::ItemDataRole{})); |
| 249 |
} |
| 250 |
|
| 251 |
QRangeModel *makeBoundedIota() |
| 252 |
{ |
| 253 |
return new QRangeModel(std::views::iota(1, 10000)); |
| 254 |
} |
| 255 |
|
| 256 |
QRangeModel *makeUnboundedIota() |
| 257 |
{ |
| 258 |
auto view = std::views::iota(1); |
| 259 |
return new QRangeModel(view); |
| 260 |
} |
| 261 |
|
| 262 |
QRangeModel *makeZipView() |
| 263 |
{ |
| 264 |
static auto x = QList<int>{1, 2, 3, 4, 5}; |
| 265 |
static auto y = std::list<QString>{"α", "β", "γ", "δ", "ε"}; |
| 266 |
static auto z = std::array<QChar, 6>{u'A', u'B', u'C', u'D', u'E', u'F'}; |
| 267 |
|
| 268 |
return new QRangeModel(std::views::zip(x, y, z)); |
| 269 |
} |
| 270 |
|
| 271 |
QRangeModel *makeGadgetTable() |
| 272 |
{ |
| 273 |
QList<QList<Gadget>> gadgetTable = { |
| 274 |
{{"1/1", Qt::red, "red"}, {"1/2", Qt::black, "black"}}, |
| 275 |
{{"2/1", Qt::blue, "blue"}, {"2/2", Qt::green, "green"}}, |
| 276 |
}; |
| 277 |
return new QRangeModel(gadgetTable); |
| 278 |
} |
| 279 |
|
| 280 |
QRangeModel *makeQtMap() |
| 281 |
{ |
| 282 |
return new QRangeModel(QMap<QString, QString>{ |
| 283 |
{"one", "eins"}, |
| 284 |
{"two", "zwei"}, |
| 285 |
{"three", "drei"}, |
| 286 |
{"four", "vier"}, |
| 287 |
}); |
| 288 |
} |
| 289 |
|
| 290 |
QRangeModel *makeStdMap() |
| 291 |
{ |
| 292 |
return new QRangeModel(std::map<int, double>{ |
| 293 |
{1, 0.1}, |
| 294 |
{2, 0.2}, |
| 295 |
{3, 0.3}, |
| 296 |
{4, 0.4}, |
| 297 |
}); |
| 298 |
} |
| 299 |
|
| 300 |
QRangeModel *makeMultiRoleMap() |
| 301 |
{ |
| 302 |
using ColorEntry = QMap<Qt::ItemDataRole, QVariant>; |
| 303 |
|
| 304 |
const QStringList colorNames = QColor::colorNames(); |
| 305 |
QList<ColorEntry> colors; |
| 306 |
colors.reserve(colorNames.size()); |
| 307 |
for (const QString &name : QColor::colorNames()) { |
| 308 |
const QColor color = QColor::fromString(name); |
| 309 |
colors << ColorEntry{{Qt::DisplayRole, name}, |
| 310 |
{Qt::DecorationRole, color}, |
| 311 |
{Qt::ToolTipRole, color.name()}}; |
| 312 |
} |
| 313 |
return new QRangeModel(colors); |
| 314 |
} |
| 315 |
|
| 316 |
QRangeModel *makeUniqueObjects() |
| 317 |
{ |
| 318 |
std::array<std::unique_ptr<Object>, 3> data = { |
| 319 |
std::make_unique<Object>(1), |
| 320 |
std::make_unique<Object>(2), |
| 321 |
std::make_unique<Object>(3), |
| 322 |
}; |
| 323 |
return new QRangeModel(std::move(data)); |
| 324 |
} |
| 325 |
|
| 326 |
QRangeModel *makeUniquePtrArray() |
| 327 |
{ |
| 328 |
// not possible, values need to be copyable |
| 329 |
// std::array<std::unique_ptr<QString>, 3> data = { |
| 330 |
// std::make_unique<QString>("A"), |
| 331 |
// std::make_unique<QString>("B"), |
| 332 |
// std::make_unique<QString>("C"), |
| 333 |
// }; |
| 334 |
// return new QRangeModel(std::move(data)); |
| 335 |
return nullptr; |
| 336 |
} |
| 337 |
|
| 338 |
QRangeModel *makeUniqueRows() |
| 339 |
{ |
| 340 |
std::array<std::unique_ptr<std::vector<QString>>, 3> data = { |
| 341 |
std::make_unique<std::vector<QString>>(std::vector<QString>{u"A"_s, u"B"_s, u"C"_s}), |
| 342 |
std::make_unique<std::vector<QString>>(std::vector<QString>{u"D"_s, u"E"_s, u"F"_s}), |
| 343 |
std::make_unique<std::vector<QString>>(std::vector<QString>{u"G"_s, u"H"_s, u"I"_s}), |
| 344 |
}; |
| 345 |
return new QRangeModel(std::move(data)); |
| 346 |
} |
| 347 |
|
| 348 |
QRangeModel *makeTree() |
| 349 |
{ |
| 350 |
static TreeRow root[] = {{"Germany", "Berlin"}, |
| 351 |
{"France", "Paris"}, |
| 352 |
{"Austria", "Vienna"} |
| 353 |
}; |
| 354 |
|
| 355 |
static Tree europe{std::make_move_iterator(std::begin(root)), std::make_move_iterator(std::end(root))}; |
| 356 |
TreeRow &bavaria = europe[0].addChild("Bavaria", "Munich"); |
| 357 |
bavaria.addChild("Upper Bavaria", "München"); |
| 358 |
bavaria.addChild("Lower Bavaria", "Landshut"); |
| 359 |
bavaria.addChild("Upper Palatinate", "Regensburg"); |
| 360 |
bavaria.addChild("Swabia", "Augsburg"); |
| 361 |
bavaria.addChild("Franconia", "Nürnberg"); |
| 362 |
bavaria.addChild("Upper Franconia", "Bayreuth"); |
| 363 |
bavaria.addChild("Middle Franconia", "Ansbach"); |
| 364 |
bavaria.addChild("Lower Franconia", "Würzburg"); |
| 365 |
|
| 366 |
TreeRow &hessia = europe[0].addChild("Hessia", "Wiesbaden"); |
| 367 |
hessia.addChild("Upper Hesse", "Giessen"); |
| 368 |
hessia.addChild("Lower Hesse", "Darmstadt"); |
| 369 |
hessia.addChild("North Hesse", "Kassel"); |
| 370 |
|
| 371 |
europe[1].addChild("Île-de-France", "Paris"); |
| 372 |
europe[1].addChild("Provence-Alpes-Côte d'Azur", "Marseille"); |
| 373 |
europe[1].addChild("Auvergne-Rhône-Alpes", "Lyon"); |
| 374 |
europe[1].addChild("Nouvelle-Aquitaine", "Bordeaux"); |
| 375 |
|
| 376 |
europe[2].addChild("Vienna", "Vienna"); |
| 377 |
europe[2].addChild("Lower Austria", "St. Pölten"); |
| 378 |
europe[2].addChild("Upper Austria", "Linz"); |
| 379 |
europe[2].addChild("Styria", "Graz"); |
| 380 |
europe[2].addChild("Carinthia", "Klagenfurt"); |
| 381 |
europe[2].addChild("Salzburg", "Salzburg"); |
| 382 |
europe[2].addChild("Tyrol", "Innsbruck"); |
| 383 |
europe[2].addChild("Vorarlberg", "Bregenz"); |
| 384 |
europe[2].addChild("Burgenland", "Eisenstadt"); |
| 385 |
|
| 386 |
return new QRangeModel(std::ref(europe)); |
| 387 |
} |
| 388 |
}; |
| 389 |
struct QMetaMethodEnumerator |
| 390 |
{ |
| 391 |
struct iterator |
| 392 |
{ |
| 393 |
using difference_type = int; |
| 394 |
using size_type = int; |
| 395 |
using pointer = void; |
| 396 |
using iterator_category = std::forward_iterator_tag; |
| 397 |
using value_type = std::tuple<int, QByteArray, QMetaMethod>; |
| 398 |
using reference = value_type; |
| 399 |
using const_reference = const value_type; |
| 400 |
|
| 401 |
friend constexpr iterator &operator++(iterator &that) |
| 402 |
{ ++that.m_index; return that; } |
| 403 |
friend constexpr iterator operator++(iterator &that, int) |
| 404 |
{ auto copy = that; ++that.m_index; return copy; } |
| 405 |
friend constexpr iterator &operator+=(iterator &that, int n) |
| 406 |
{ that.m_index += n; return that; } |
| 407 |
|
| 408 |
friend constexpr bool comparesEqual(const iterator &lhs, const iterator &rhs) noexcept |
| 409 |
{ |
| 410 |
return lhs.m_index == rhs.m_index && lhs.m_metaobject == rhs.m_metaobject; |
| 411 |
} |
| 412 |
|
| 413 |
friend constexpr Qt::strong_ordering compareThreeWay(const iterator &lhs, |
| 414 |
const iterator &rhs) noexcept |
| 415 |
{ |
| 416 |
return Qt::compareThreeWay(lhs.m_index, rhs.m_index); |
| 417 |
} |
| 418 |
Q_DECLARE_STRONGLY_ORDERED(iterator) |
| 419 |
|
| 420 |
const_reference operator*() const |
| 421 |
{ return {m_index, |
| 422 |
m_metaobject->method(m_index).name().slice(4), |
| 423 |
m_metaobject->method(m_index)}; } |
| 424 |
|
| 425 |
const QMetaObject *m_metaobject = nullptr; |
| 426 |
int m_index = 0; |
| 427 |
}; |
| 428 |
|
| 429 |
static_assert(std::input_iterator<iterator>); |
| 430 |
|
| 431 |
using size_type = iterator::size_type; |
| 432 |
using value_type = iterator::value_type; |
| 433 |
using const_iterator = iterator; |
| 434 |
|
| 435 |
const_iterator begin() const { return iterator{&m_metaobject, m_metaobject.methodOffset()}; } |
| 436 |
const_iterator end() const { return iterator{&m_metaobject, size()}; } |
| 437 |
size_type size() const { return m_metaobject.methodCount() - m_metaobject.methodOffset(); } |
| 438 |
|
| 439 |
explicit QMetaMethodEnumerator(const QMetaObject &mo) noexcept |
| 440 |
: m_metaobject(mo) |
| 441 |
{} |
| 442 |
|
| 443 |
template <typename Class> |
| 444 |
static QMetaMethodEnumerator fromType() noexcept |
| 445 |
{ |
| 446 |
return QMetaMethodEnumerator{Class::staticMetaObject}; |
| 447 |
} |
| 448 |
|
| 449 |
private: |
| 450 |
const QMetaObject &m_metaobject; |
| 451 |
}; |
| 452 |
|
| 453 |
class MainWindow : public QMainWindow |
| 454 |
{ |
| 455 |
Q_OBJECT |
| 456 |
public: |
| 457 |
MainWindow(QWidget *parent = nullptr) |
| 458 |
: QMainWindow(parent), model(nullptr) |
| 459 |
{ |
| 460 |
QSplitter *splitter = new QSplitter; |
| 461 |
|
| 462 |
treeview = new QTreeView; |
| 463 |
treeview->setUniformRowHeights(true); |
| 464 |
splitter->addWidget(treeview); |
| 465 |
|
| 466 |
#ifdef QUICK_UI |
| 467 |
quickWidget = new QQuickWidget; |
| 468 |
quickWidget->loadFromModule("Main", "Main"); |
| 469 |
splitter->addWidget(quickWidget); |
| 470 |
#endif |
| 471 |
|
| 472 |
setCentralWidget(splitter); |
| 473 |
|
| 474 |
QComboBox *modelPicker = new QComboBox; |
| 475 |
connect(modelPicker, &QComboBox::currentIndexChanged, this, &MainWindow::modelChanged); |
| 476 |
// this will implicitly run modelChanged() |
| 477 |
modelPicker->setModel(new QRangeModel(QMetaMethodEnumerator::fromType<ModelFactory>(), |
| 478 |
modelPicker)); |
| 479 |
modelPicker->setModelColumn(1); |
| 480 |
|
| 481 |
QToolBar *toolBar = addToolBar(tr("Model Operations")); |
| 482 |
toolBar->addWidget(modelPicker); |
| 483 |
|
| 484 |
QAction *addAction = toolBar->addAction(tr("Add"), this, &MainWindow::onAdd); |
| 485 |
addAction->setIcon(QIcon::fromTheme(QIcon::ThemeIcon::ListAdd)); |
| 486 |
QAction *removeAction = toolBar->addAction(tr("Remove"), this, &MainWindow::onRemove); |
| 487 |
removeAction->setIcon(QIcon::fromTheme(QIcon::ThemeIcon::ListRemove)); |
| 488 |
QAction *upAction = toolBar->addAction(tr("Move up"), this, &MainWindow::onUp); |
| 489 |
upAction->setIcon(QIcon::fromTheme(QIcon::ThemeIcon::GoUp)); |
| 490 |
QAction *downAction = toolBar->addAction(tr("Move down"), this, &MainWindow::onDown); |
| 491 |
downAction->setIcon(QIcon::fromTheme(QIcon::ThemeIcon::GoDown)); |
| 492 |
QAction *indentAction = toolBar->addAction(tr("Move in"), this, &MainWindow::onIn); |
| 493 |
indentAction->setIcon(QIcon::fromTheme(QIcon::ThemeIcon::FormatIndentMore)); |
| 494 |
QAction *dedentAction = toolBar->addAction(tr("Move out"), this, &MainWindow::onOut); |
| 495 |
dedentAction->setIcon(QIcon::fromTheme(QIcon::ThemeIcon::FormatIndentLess)); |
| 496 |
} |
| 497 |
|
| 498 |
private: |
| 499 |
void modelChanged(int index) |
| 500 |
{ |
| 501 |
QAbstractItemModel *oldModel = model; |
| 502 |
|
| 503 |
const QMetaObject &mo = ModelFactory::staticMetaObject; |
| 504 |
const QMetaMethod method = mo.method(index + mo.methodOffset()); |
| 505 |
if (method.invoke(&factory, qReturnArg(model))) { |
| 506 |
treeview->setModel(model); |
| 507 |
#ifdef QUICK_UI |
| 508 |
if (!quickWidget->rootObject()) |
| 509 |
statusBar()->showMessage(tr("Failed to load QML")); |
| 510 |
else |
| 511 |
quickWidget->rootObject()->setProperty("model", QVariant::fromValue(model)); |
| 512 |
#endif |
| 513 |
} |
| 514 |
|
| 515 |
delete oldModel; |
| 516 |
} |
| 517 |
|
| 518 |
void onAdd() |
| 519 |
{ |
| 520 |
const auto current = treeview->currentIndex(); |
| 521 |
showMessage(tr("Inserting after '%1'").arg(current.data().toString())); |
| 522 |
if (!model->insertRows(current.row() + 1, 1, current.parent())) { |
| 523 |
showMessage(tr("Insertion failed")); |
| 524 |
} else { |
| 525 |
const auto newIndex = model->index(current.row() + 1, 0, current.parent()); |
| 526 |
static int counter = 0; |
| 527 |
model->setData(newIndex, u"New Value %1"_s.arg(++counter)); |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
void onRemove() |
| 532 |
{ |
| 533 |
const auto current = treeview->currentIndex(); |
| 534 |
showMessage(tr("Removing '%1'").arg(current.data().toString())); |
| 535 |
if (!model->removeRows(current.row(), 1, model->parent(current))) |
| 536 |
showMessage(tr("Removal failed")); |
| 537 |
} |
| 538 |
|
| 539 |
void onUp() |
| 540 |
{ |
| 541 |
const auto current = treeview->currentIndex(); |
| 542 |
showMessage(tr("Moving '%1' up").arg(current.data().toString())); |
| 543 |
const auto currentParent = current.parent(); |
| 544 |
if (!model->moveRows(currentParent, current.row(), 1, currentParent, current.row() - 1)) |
| 545 |
showMessage(tr("Failed to move up")); |
| 546 |
} |
| 547 |
|
| 548 |
void onDown() |
| 549 |
{ |
| 550 |
const auto current = treeview->currentIndex(); |
| 551 |
showMessage(tr("Moving '%1' down").arg(current.data().toString())); |
| 552 |
const auto currentParent = current.parent(); |
| 553 |
if (!model->moveRows(currentParent, current.row(), 1, currentParent, current.row() + 2)) |
| 554 |
showMessage(tr("Failed to move down")); |
| 555 |
} |
| 556 |
|
| 557 |
void onIn() |
| 558 |
{ |
| 559 |
const auto current = treeview->currentIndex(); |
| 560 |
showMessage(tr("Moving '%1' in").arg(current.data().toString())); |
| 561 |
const auto currentParent = current.parent(); |
| 562 |
const auto newParent = current.sibling(current.row() - 1, 0); |
| 563 |
// move the selected row under it's top-most sibling |
| 564 |
|
| 565 |
if (!model->moveRows(currentParent, current.row(), 1, newParent, model->rowCount(newParent))) |
| 566 |
showMessage(tr("Indentation failed")); |
| 567 |
} |
| 568 |
|
| 569 |
void onOut() |
| 570 |
{ |
| 571 |
const auto current = treeview->currentIndex(); |
| 572 |
showMessage(tr("Moving '%1' out").arg(current.data().toString())); |
| 573 |
const auto currentParent = current.parent(); |
| 574 |
const auto grandParent = currentParent.parent(); |
| 575 |
// move the selected row under it's grandparent |
| 576 |
if (!model->moveRows(currentParent, current.row(), 1, grandParent, currentParent.row())) |
| 577 |
showMessage(tr("Dedentation failed")); |
| 578 |
} |
| 579 |
|
| 580 |
void showMessage(const QString &message, int timeout = 2000) |
| 581 |
{ |
| 582 |
statusBar()->showMessage(message, timeout); |
| 583 |
} |
| 584 |
|
| 585 |
ModelFactory factory; |
| 586 |
QRangeModel *model; |
| 587 |
QTreeView *treeview; |
| 588 |
#ifdef QUICK_UI |
| 589 |
QQuickWidget *quickWidget; |
| 590 |
#endif |
| 591 |
}; |
| 592 |
|
| 593 |
int main(int argc, char *argv[]) |
| 594 |
{ |
| 595 |
QApplication app(argc, argv); |
| 596 |
|
| 597 |
auto mainWindow = std::make_unique<MainWindow>(); |
| 598 |
mainWindow->show(); |
| 599 |
|
| 600 |
return app.exec(); |
| 601 |
} |
| 602 |
|
| 603 |
#include "main.moc" |