#pragma GCC diagnostic ignored "-Wdeprecated-declarations" #include #pragma GCC diagnostic pop #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using namespace Wt; using namespace Wt::cpp17; #define SET_BS5THEME 1 // Always apply theme before creating widgets! class CThemedApplication : public WApplication { public: CThemedApplication(const WEnvironment& rcEnv) : WApplication{rcEnv} { #if SET_BS5THEME setTheme(make_shared()); #endif } }; class CFixedTableModel : public WAbstractTableModel { public: CFixedTableModel(const int rowCount, const int colCount) : _rowCount{rowCount} , _colCount{colCount} { } any headerData(const int section, const Orientation orientation, const ItemDataRole role) const { if (orientation != Orientation::Horizontal) throw invalid_argument("orientation"); switch (role.value()) { default: return {}; case ItemDataRole::Display: return format("C{}", section); } } any data(const WModelIndex& index, const ItemDataRole role) const override { if (!index.isValid()) return {}; switch (role.value()) { default: return {}; case ItemDataRole::Display: return format("{}, {}", index.row(), index.column()); } } //! Returns the WModelIndex moved by delta. [[nodiscard]] WModelIndex move(const WModelIndex& index, int rowDelta, int colDelta) { const int row{clamp(index.row() + rowDelta, 0, _rowCount - 1)}; const int col{clamp(index.column() + colDelta, 0, _colCount - 1)}; return createIndex(row, col, nullptr); } int columnCount(const WModelIndex& index) const override { return index.isValid() ? 0 : _colCount; } int rowCount (const WModelIndex& index) const override { return index.isValid() ? 0 : _rowCount; } const int _rowCount; const int _colCount; }; template constexpr inline std::unique_ptr create(std::function&& fInitialize, TArgs&&... args) { std::unique_ptr Ptr{std::make_unique(std::forward(args)...)}; fInitialize(*Ptr); return Ptr; } inline void setAccessKey(WInteractWidget& widget, const char mnemonic) { widget.setAttributeValue("accesskey", string(1, tolower(mnemonic))); } class CApplication final : public CThemedApplication { public: CApplication(const WEnvironment& rcEnv); private: WServer& _Server; //!< The associated WServer instance, needed for async operations. WContainerWidget& _cntRoot{*root()}; WBoxLayout& _lytRoot{*_cntRoot.setLayout(make_unique())}; WPushButton& _btnP{*_lytRoot.addWidget(create([this](auto& btnP) { btnP.clicked().connect([this] { _pnlBottom.show(); }); btnP.setTextFormat(TextFormat::UnsafeXHTML); setAccessKey(btnP, 'p'); }, "Show panel"))}; WTabWidget& _tbwMain{*_lytRoot.addWidget(make_unique(), /*stretch*/ 100)}; WMenuItem& _tabA{*_tbwMain.addTab(make_unique(), "A", ContentLoading::Eager)}; WTableView& _tvA{dynamic_cast(*_tbwMain.widget(_tbwMain.count() - 1))}; WMenuItem& _tabB{*_tbwMain.addTab(make_unique(), "B", ContentLoading::Eager)}; WTableView& _tvB{dynamic_cast(*_tbwMain.widget(_tbwMain.count() - 1))}; WPanel& _pnlBottom{*_lytRoot.addWidget(make_unique())}; WContainerWidget& _cntBottom{*_pnlBottom.setCentralWidget(make_unique())}; WBoxLayout& _lytBottom{*_cntBottom.setLayout(make_unique())}; WPushButton& _btnA{*_lytBottom.addWidget(create([this](auto& btnA) { btnA.clicked().connect([this] { _tvA.setColumnWidth(0, 50); }); btnA.setTextFormat(TextFormat::UnsafeXHTML); setAccessKey(btnA, '0'); }, "Set C0 width"))}; WPushButton& _btnB{*_lytBottom.addWidget(create([this](auto& btnB) { btnB.clicked().connect([this] { _tvA.setColumnWidth(1, 50); }); btnB.setTextFormat(TextFormat::UnsafeXHTML); setAccessKey(btnB, '1'); }, "Set C1 width"))}; WPushButton& _btnC{*_lytBottom.addWidget(create([this](auto& btnC) { btnC.clicked().connect([this] { _tvA.setColumnWidth(2, 50); }); btnC.setTextFormat(TextFormat::UnsafeXHTML); setAccessKey(btnC, '2'); }, "Set C2 width"))}; shared_ptr _mdlAPtr{make_shared(100, 50)}; shared_ptr _mdlBPtr{make_shared(200, 100)}; const vector> _appWidgets{_tabA, _tabB, _pnlBottom}; }; CApplication::CApplication(const WEnvironment& rcEnv) : CThemedApplication{rcEnv} , _Server{*environment().server()} { setTitle("bug repro"); const auto initTable = [](WTableView& tv, const shared_ptr& mdlPtr) { tv.setSelectionBehavior(SelectionBehavior::Items); tv.setSelectionMode(SelectionMode::Single); tv.setSortingEnabled(false); tv.setCanReceiveFocus(true); tv.setEditTriggers(EditTrigger::None); tv.setAlternatingRowColors(true); tv.setModel(mdlPtr); tv.setRowHeaderCount(1); tv.selectionChanged().connect([&tv] { const WModelIndexSet selectedIndexes{tv.selectionModel()->selectedIndexes()}; switch (selectedIndexes.size()) { case 0: break; case 1: tv.scrollTo(*selectedIndexes.cbegin()); break; default: throw logic_error("multi-selection unexpected"); } }); tv.select(mdlPtr->move(WModelIndex{}, 0, 1)); }; initTable(_tvA, _mdlAPtr); initTable(_tvB, _mdlBPtr); _lytBottom.setContentsMargins(0, 0, 0, 0); _lytBottom.addStretch(100); _tvA.setColumnWidth(0, 70); _Server.schedule(1s, sessionId(), [this] { _tvA.setColumnWidth(0, 60); triggerUpdate(); }); // final action after creating the UI enableUpdates(); } int main(int argc, char* argv[]) { return WRun(argc, argv, [](const WEnvironment& rcEnv) { try { return make_unique(rcEnv); } catch (const std::exception& rcException) { cerr << "exception " << typeid(remove_cvref::type).name() << ": " << rcException.what() << endl; #if DEBUG exit(-1); #endif throw; } }); }