diff --git a/src/osltoy/osltoyapp.cpp b/src/osltoy/osltoyapp.cpp index 2fee706bb..70cca5b8c 100644 --- a/src/osltoy/osltoyapp.cpp +++ b/src/osltoy/osltoyapp.cpp @@ -355,36 +355,29 @@ class OSLToyRenderView final : public QLabel { class OSLToySearchPathLine final : public QLineEdit { // Q_OBJECT public: - explicit OSLToySearchPathLine(OSLToySearchPathEditor* editor, int index); - bool previouslyHadContent() const { - return m_previouslyHadContent; - } + bool previouslyHadContent() const { return m_previouslyHadContent; } - void setPreviouslyHadContent(bool value) { - m_previouslyHadContent = value; - } + void setPreviouslyHadContent(bool value) { m_previouslyHadContent = value; } - int getIndex() const { - return m_index; - } + int getIndex() const { return m_index; } QSize sizeHint() const override; private: - - static QColor getColor(int index) { + static QColor getColor(int index) + { if (index % 2) - return QColor(0xFFE0F0FF); // light blue + return QColor(0xFFE0F0FF); // light blue else return Qt::white; } bool m_previouslyHadContent = false; - int m_index; + int m_index; OSLToySearchPathEditor* m_editor = nullptr; }; @@ -393,75 +386,83 @@ class OSLToySearchPathLine final : public QLineEdit { class OSLToySearchPathEditor final : public QWidget { // Q_OBJECT - using UpdatePathListAction = std::function&)>; + using UpdatePathListAction + = std::function&)>; + public: + OSLToySearchPathEditor(QWidget* parent, + UpdatePathListAction updatePathsAction) + : QWidget(parent, static_cast( + Qt::Tool | Qt::WindowStaysOnTopHint)) + , m_lines() + , m_updateAction(updatePathsAction) + { + window()->setWindowTitle(tr("#include Search Path List")); - OSLToySearchPathEditor(QWidget* parent, UpdatePathListAction updatePathsAction) - : QWidget(parent, static_cast(Qt::Tool | Qt::WindowStaysOnTopHint)), - m_lines(), - m_updateAction(updatePathsAction) - { - window()->setWindowTitle(tr("#include Search Path List")); + int thisWidth = parent->width(); // / 3; + int thisHeight = parent->height(); // / 4; - int thisWidth = parent->width(); // / 3; - int thisHeight = parent->height(); // / 4; + resize(thisWidth, thisHeight); - resize(thisWidth, thisHeight); + setFixedSize(size()); - setFixedSize(size()); + class MyScrollArea : public QScrollArea { + QWidget* m_parent; - class MyScrollArea : public QScrollArea { - QWidget* m_parent; - public: - explicit MyScrollArea(QWidget* parent) : QScrollArea(parent), m_parent(parent) { } + public: + explicit MyScrollArea(QWidget* parent) + : QScrollArea(parent), m_parent(parent) + { + } - QSize sizeHint() const override { - return m_parent->size(); - } - }; + QSize sizeHint() const override { return m_parent->size(); } + }; - class MyFrame : public QFrame { - QWidget* m_parent; - public: - explicit MyFrame(QWidget* parent) : QFrame(parent), m_parent(parent) { } + class MyFrame : public QFrame { + QWidget* m_parent; - QSize sizeHint() const override { - return m_parent->size(); - } - }; + public: + explicit MyFrame(QWidget* parent) : QFrame(parent), m_parent(parent) + { + } - auto scroll_area = new MyScrollArea(this); - scroll_area->setWidgetResizable(true); + QSize sizeHint() const override { return m_parent->size(); } + }; - auto frame = new MyFrame(scroll_area); - // frame->setWidgetResizable(true); + auto scroll_area = new MyScrollArea(this); + scroll_area->setWidgetResizable(true); - auto layout = new QVBoxLayout(); - layout->setSpacing(0); - // int topMargin = thisWidth / 6; - // layout->setContentsMargins(topMargin / 2, topMargin, topMargin / 2, topMargin / 2); + auto frame = new MyFrame(scroll_area); + // frame->setWidgetResizable(true); - frame->setLayout(layout); + auto layout = new QVBoxLayout(); + layout->setSpacing(0); + // int topMargin = thisWidth / 6; + // layout->setContentsMargins(topMargin / 2, topMargin, topMargin / 2, topMargin / 2); - frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + frame->setLayout(layout); - scroll_area->setWidget(frame); + frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); - m_layout = layout; + scroll_area->setWidget(frame); - scroll_area->show(); - m_scrollArea = scroll_area; - } + m_layout = layout; + scroll_area->show(); + m_scrollArea = scroll_area; + } - void set_path_list(const std::vector& paths) { + void set_path_list(const std::vector& paths) + { while (!m_lines.empty()) pop_line(); - m_maxIndexWithContent = (int)paths.size() - 1; // ok that this is -1 if the paths are empty + m_maxIndexWithContent + = (int)paths.size() + - 1; // ok that this is -1 if the paths are empty @@ -481,24 +482,24 @@ class OSLToySearchPathEditor final : public QWidget { } - void observe_changed_text() { + void observe_changed_text() + { // Only listen to signals from OSLToySearchPathLine objects if (auto changedLine = dynamic_cast(sender())) { bool isNowEmpty = changedLine->text().isEmpty(); if (changedLine->previouslyHadContent() && isNowEmpty) { if (changedLine->getIndex() == m_maxIndexWithContent) { - // Find the next max index with content, or -1 if none. do { --m_maxIndexWithContent; - } while(m_maxIndexWithContent >= 0 && - !m_lines[m_maxIndexWithContent]->previouslyHadContent()); + } while (m_maxIndexWithContent >= 0 + && !m_lines[m_maxIndexWithContent] + ->previouslyHadContent()); shrink_as_needed(); } - } - else if (!changedLine->previouslyHadContent() && !isNowEmpty) { + } else if (!changedLine->previouslyHadContent() && !isNowEmpty) { if (changedLine->getIndex() > m_maxIndexWithContent) { m_maxIndexWithContent = changedLine->getIndex(); grow_as_needed(); @@ -510,8 +511,8 @@ class OSLToySearchPathEditor final : public QWidget { } protected: - - void closeEvent(QCloseEvent *ev) override { + void closeEvent(QCloseEvent* ev) override + { // On close, collate the list of search paths, and if there has been any change, update. bool has_updated = false; @@ -530,26 +531,23 @@ class OSLToySearchPathEditor final : public QWidget { private: - - - - void push_line() + void push_line() { auto l = new OSLToySearchPathLine(this, (int)m_lines.size()); - + m_layout->addWidget(l); m_lines.push_back(l); } - void pop_line() + void pop_line() { auto line = m_lines.back(); m_lines.pop_back(); m_layout->removeWidget(line); } - void update_path_list() + void update_path_list() { std::vector path_list; for (auto line : m_lines) { @@ -560,11 +558,15 @@ class OSLToySearchPathEditor final : public QWidget { m_updateAction(path_list); } - size_t required_lines() const { - return static_cast((std::max)(m_minLineCount, m_maxIndexWithContent + m_guaranteedEmptyLineCount + 1)); + size_t required_lines() const + { + return static_cast( + (std::max)(m_minLineCount, + m_maxIndexWithContent + m_guaranteedEmptyLineCount + 1)); } - void grow_as_needed() { + void grow_as_needed() + { auto newReqLines = required_lines(); while (m_lines.size() < newReqLines) { @@ -572,7 +574,8 @@ class OSLToySearchPathEditor final : public QWidget { } } - void shrink_as_needed() { + void shrink_as_needed() + { auto newReqLines = required_lines(); while (m_lines.size() > newReqLines) { @@ -582,39 +585,40 @@ class OSLToySearchPathEditor final : public QWidget { - int m_minLineCount = 12; + int m_minLineCount = 12; int m_guaranteedEmptyLineCount = 5; - int m_maxIndexWithContent = -1; + int m_maxIndexWithContent = -1; std::vector m_lines; - QLayout* m_layout = nullptr; + QLayout* m_layout = nullptr; QScrollArea* m_scrollArea = nullptr; UpdatePathListAction m_updateAction; }; -OSLToySearchPathLine::OSLToySearchPathLine(OSLToySearchPathEditor* editor, int index) - : QLineEdit(), - m_index(index), - m_editor(editor) - { - setFrame(true); +OSLToySearchPathLine::OSLToySearchPathLine(OSLToySearchPathEditor* editor, + int index) + : QLineEdit(), m_index(index), m_editor(editor) +{ + setFrame(true); - auto p = this->palette(); - p.setColor(QPalette::Base, getColor(index)); - setPalette(p); + auto p = this->palette(); + p.setColor(QPalette::Base, getColor(index)); + setPalette(p); - setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); + setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); - QObject::connect(this, &OSLToySearchPathLine::editingFinished, - editor, &OSLToySearchPathEditor::observe_changed_text); + QObject::connect(this, &OSLToySearchPathLine::editingFinished, editor, + &OSLToySearchPathEditor::observe_changed_text); - // Maybe add a QCompleter that completes known paths - // setCompletion - show(); - } + // Maybe add a QCompleter that completes known paths + // setCompletion + show(); +} -QSize OSLToySearchPathLine::sizeHint() const { +QSize +OSLToySearchPathLine::sizeHint() const +{ return QSize(m_editor->width() - 4, 10); } @@ -725,9 +729,10 @@ OSLToyMainWindow::OSLToyMainWindow(OSLToyRenderer* rend, int xr, int yr) &OSLToyMainWindow::restart_time); control_area_layout->addWidget(restartButton); - searchPathEditor = new OSLToySearchPathEditor(this, [this](auto&& paths) mutable { - update_include_search_paths(paths); - }); + searchPathEditor + = new OSLToySearchPathEditor(this, [this](auto&& paths) mutable { + update_include_search_paths(paths); + }); auto editorarea = new QWidget; QFontMetrics fontmetrics(CodeEditor::fixedFont()); @@ -790,9 +795,8 @@ OSLToyMainWindow::createActions() &OSLToyMainWindow::action_fullscreen); - add_action("search-path-popup", - "Edit #include search paths...", - "Shift-Ctrl+P", + add_action("search-path-popup", "Edit #include search paths...", + "Shift-Ctrl+P", &OSLToyMainWindow::action_open_search_path_popup); } @@ -989,17 +993,17 @@ OSLToyMainWindow::open_file(const std::string& filename) } -void -OSLToyMainWindow::set_include_search_paths(const std::vector& paths) +void +OSLToyMainWindow::set_include_search_paths(const std::vector& paths) { searchPathEditor->set_path_list(paths); } -void -OSLToyMainWindow::update_include_search_paths(const std::vector& paths) +void +OSLToyMainWindow::update_include_search_paths( + const std::vector& paths) { - - m_include_search_paths = paths; + m_include_search_paths = paths; m_should_regenerate_compile_options = true; // Open question: Do we want to force a recompile whenever the list is updated? @@ -1053,7 +1057,9 @@ OSLToyMainWindow::action_save() -void OSLToyMainWindow::action_open_search_path_popup () { +void +OSLToyMainWindow::action_open_search_path_popup() +{ auto centeredXPos = x() + (width() - searchPathEditor->width()) / 2; auto centeredYPos = y() + (height() - searchPathEditor->height()) / 2; searchPathEditor->move(centeredXPos, centeredYPos); @@ -1140,18 +1146,18 @@ class MyOSLCErrorHandler final : public OIIO::ErrorHandler { }; -void +void OSLToyMainWindow::regenerate_compile_options() { // Right now, the only option we consider is include search path (-I) - - // Annoyingly, oslcomp only supports -I flags without any seperator between + + // Annoyingly, oslcomp only supports -I flags without any seperator between // the -I and the path itself, but OIIO::ArgParse does not support parsing // arguments in this manner. Oy vey. m_compile_options.clear(); - for (auto&& path : m_include_search_paths) + for (auto&& path : m_include_search_paths) m_compile_options.push_back(std::string("-I").append(path)); @@ -1189,14 +1195,13 @@ OSLToyMainWindow::recompile_shaders() if (m_should_regenerate_compile_options) regenerate_compile_options(); - ok = oslcomp.compile_buffer(source, osooutput, m_compile_options, "", - briefname); + ok = oslcomp.compile_buffer(source, osooutput, m_compile_options, + "", briefname); auto error_message = OIIO::Strutil::fmt::format( - "{}\n\nCompiled {} with options: {}", - OIIO::Strutil::join(errhandler.errors, "\n"), - briefname, - OIIO::Strutil::join(m_compile_options, " ")); + "{}\n\nCompiled {} with options: {}", + OIIO::Strutil::join(errhandler.errors, "\n"), briefname, + OIIO::Strutil::join(m_compile_options, " ")); set_error_message(tab, error_message); if (ok) { diff --git a/src/osltoy/osltoyapp.h b/src/osltoy/osltoyapp.h index a1e3b775a..14a0d3f40 100644 --- a/src/osltoy/osltoyapp.h +++ b/src/osltoy/osltoyapp.h @@ -106,12 +106,12 @@ private slots: // Non-owning pointers to all the widgets we create. Qt is responsible // for deleting. QSplitter* centralSplitter; - OSLToyRenderView* renderView = nullptr; + OSLToyRenderView* renderView = nullptr; OSLToySearchPathEditor* searchPathEditor = nullptr; - QTabWidget* textTabs = nullptr; - QScrollArea* paramScroll = nullptr; - QWidget* paramWidget = nullptr; - QGridLayout* paramLayout = nullptr; + QTabWidget* textTabs = nullptr; + QScrollArea* paramScroll = nullptr; + QWidget* paramWidget = nullptr; + QGridLayout* paramLayout = nullptr; QLabel* statusFPS; QMenu *fileMenu, *editMenu, *viewMenu, *toolsMenu, *helpMenu; QPushButton *recompileButton, *pauseButton, *restartButton;