Skip to content

Commit

Permalink
TabControl tab rename command
Browse files Browse the repository at this point in the history
  • Loading branch information
niello committed Sep 12, 2021
1 parent deca2f0 commit c7c2f3a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
34 changes: 34 additions & 0 deletions src/ui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
#include "QtnProperty/PropertyWidget.h"
#include <qclipboard.h>

// TODO: here for now, move to more appropriate place once it is created
#include "QtnProperty/PropertyView.h"
#include <qtimer.h>

// FIXME QTBUG: Qt 5.13.0 text rendering in OpenGL breaks on QOpenGLWidget delete
#include <qopenglwidget.h>
// FIXME: read-only QLineEdit passes by Backspace and Delete, so shortcuts work when they must not
Expand Down Expand Up @@ -1327,3 +1331,33 @@ void MainWindow::openNewEditor(EditorBasePtr editor)

connect(editorPtr, &EditorBase::contentsChanged, this, &MainWindow::onEditorContentsChanged);
}

bool MainWindow::focusOnProperty(const QString& name, bool startEdit) const
{
auto propertyWidget = static_cast<QtnPropertyWidget*>(propertyDockWidget->widget());
auto props = propertyWidget->propertySet()->findChildProperties(name);
return !props.empty() && focusOnProperty(*props.begin(), startEdit);
}

bool MainWindow::focusOnProperty(QtnPropertyBase* prop, bool startEdit) const
{
if (!prop) return false;

auto propertyWidget = static_cast<QtnPropertyWidget*>(propertyDockWidget->widget());
propertyWidget->propertyView()->setActiveProperty(prop, true);
propertyWidget->propertyView()->setFocus();

if (startEdit)
{
QTimer::singleShot(0, [propertyWidget]()
{
// TODO: check if application is exiting!

// Async to let the property delegate redraw sub-items. Otherwise will not work.
QKeyEvent startEdit(QKeyEvent::Type::KeyPress, Qt::Key_Return, Qt::NoModifier);
QApplication::sendEvent(propertyWidget->propertyView(), &startEdit);
});
}

return true;
}
5 changes: 5 additions & 0 deletions src/ui/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class UndoViewer;
class SettingsDialog;
class RecentlyUsedMenuEntry;
class CEGUIProject;
class QtnPropertyBase;
typedef std::unique_ptr<class EditorBase> EditorBasePtr;
typedef std::unique_ptr<class EditorFactoryBase> EditorFactoryBasePtr;

Expand Down Expand Up @@ -51,6 +52,10 @@ class MainWindow : public QMainWindow
QWidget* allocateOpenGLWidget();
void freeOpenGLWidget(QWidget* widget);

// TODO: here for now, move to more appropriate place once it is created
bool focusOnProperty(const QString& name, bool startEdit = true) const;
bool focusOnProperty(QtnPropertyBase* prop, bool startEdit = true) const;

private slots:

void openEditorTab(const QString& absolutePath);
Expand Down
37 changes: 32 additions & 5 deletions src/ui/layout/LayoutScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
#include <CEGUI/GUIContext.h>
#include <CEGUI/widgets/TabControl.h>
#include <CEGUI/widgets/TabButton.h>
#include "qgraphicssceneevent.h"
#include "qevent.h"
#include "qmimedata.h"
#include "qtreeview.h"
#include "qstandarditemmodel.h"
#include <qgraphicssceneevent.h>
#include <qevent.h>
#include <qmimedata.h>
#include <qtreeview.h>
#include <qstandarditemmodel.h>
#include <qmenu.h>
#include <set>

Expand Down Expand Up @@ -165,6 +165,33 @@ void LayoutScene::setupActionsForTabControl()
_widgetActions["CEGUI/TabControl"].emplace_back(action, conditionTabButtonUnderCursor);
}

if (auto action = new QAction("Rename Tab", _contextMenu))
{
action->setToolTip("Rename the tab under the cursor");
action->setStatusTip("Rename the tab under the cursor");
connect(action, &QAction::triggered, [this]()
{
if (!_contextMenuWidget) return;
auto tabCtl = dynamic_cast<CEGUI::TabControl*>(_contextMenuWidget->getWidget());
if (!tabCtl) return;
auto child = tabCtl->getTargetChildAtPosition(glm::vec2(_contextMenuPos.x(), _contextMenuPos.y()));
auto tabBtn = dynamic_cast<CEGUI::TabButton*>(child);
if (!tabBtn) return;
auto tab = getManipulatorByPath(CEGUIUtils::stringToQString(tabBtn->getTargetWindow()->getNamePath()));
if (!tab) return;

auto props = tab->getPropertySet()->findChildProperties("Text");
if (!props.empty())
{
clearSelection();
tab->setSelected(true);
qobject_cast<Application*>(qApp)->getMainWindow()->focusOnProperty(*props.begin());
}
});

_widgetActions["CEGUI/TabControl"].emplace_back(action, conditionTabButtonUnderCursor);
}

if (auto action = new QAction("Goto Prev Tab", _contextMenu))
{
action->setToolTip("Switch to the previous tab in the current TabControl");
Expand Down

0 comments on commit c7c2f3a

Please sign in to comment.