Skip to content

Commit

Permalink
Example running a dialog before the main window (#407)
Browse files Browse the repository at this point in the history
* Example running a dialog before the main window

Signed-off-by: Louise Poubel <louise@openrobotics.org>

* Revert FIXMEs

Signed-off-by: Louise Poubel <louise@openrobotics.org>
  • Loading branch information
chapulina authored Jun 30, 2022
1 parent f29fe15 commit 27afc95
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 1 deletion.
25 changes: 25 additions & 0 deletions examples/standalone/start_dialog/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
cmake_minimum_required(VERSION 3.10.2 FATAL_ERROR)

project(gz-gui-start-dialog)

if(POLICY CMP0100)
cmake_policy(SET CMP0100 NEW)
endif()

set(CMAKE_AUTOMOC ON)

find_package(ignition-gui3 REQUIRED)
set(GZ_GUI_VER ${ignition-gui3_VERSION_MAJOR})

set(EXEC_NAME "start_dialog")

QT5_ADD_RESOURCES(resources_RCC ${EXEC_NAME}.qrc)

add_executable(${EXEC_NAME}
${EXEC_NAME}.cc
${resources_RCC}
)
target_link_libraries(${EXEC_NAME}
ignition-gui${GZ_GUI_VER}::ignition-gui${GZ_GUI_VER}
)

16 changes: 16 additions & 0 deletions examples/standalone/start_dialog/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Example for how to run a start dialog before the main window.

## Build

cd <this directory>
mkdir build
cd build
cmake ..
make

## Run

cd <this directory>/build
./start_dialog

First the dialog shows up, and after that's closed, the main window shows up.
90 changes: 90 additions & 0 deletions examples/standalone/start_dialog/start_dialog.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (C) 2022 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include <iostream>

#include <ignition/gui/qt.h>
#include <ignition/gui/Application.hh>
#include <ignition/gui/Dialog.hh>
#include <ignition/gui/MainWindow.hh>

//////////////////////////////////////////////////
int main(int _argc, char **_argv)
{
// Increase verboosity so we see all messages
ignition::common::Console::SetVerbosity(4);

// Create app
ignition::gui::Application app(_argc, _argv, ignition::gui::WindowType::kDialog);

igndbg << "Open dialog" << std::endl;

// Add and display a dialog
auto dialog = new ignition::gui::Dialog();
dialog->QuickWindow();

std::string qmlFile(":start_dialog/start_dialog.qml");
if (!QFile(QString::fromStdString(qmlFile)).exists())
{
ignerr << "Can't find [" << qmlFile
<< "]. Are you sure it was added to the .qrc file?" << std::endl;
return -1;
}

QQmlComponent dialogComponent(ignition::gui::App()->Engine(),
QString(QString::fromStdString(qmlFile)));
if (dialogComponent.isError())
{
std::stringstream errors;
errors << "Failed to instantiate QML file [" << qmlFile << "]."
<< std::endl;
for (auto error : dialogComponent.errors())
{
errors << "* " << error.toString().toStdString() << std::endl;
}
ignerr << errors.str();
return -1;
}

auto dialogItem = qobject_cast<QQuickItem *>(dialogComponent.create());
if (!dialogItem)
{
ignerr << "Failed to instantiate QML file [" << qmlFile << "]." << std::endl
<< "Are you sure the file is valid QML? "
<< "You can check with the `qmlscene` tool" << std::endl;
return -1;
}

dialogItem->setParentItem(dialog->RootItem());

// Execute start dialog
app.exec();

// After dialog is shut, display the main window
igndbg << "Dialog closed, open main window" << std::endl;

// Create main window
app.CreateMainWindow();

// Run main window
app.exec();

igndbg << "Main window closed" << std::endl;

return 0;
}

26 changes: 26 additions & 0 deletions examples/standalone/start_dialog/start_dialog.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2022 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import QtQuick 2.0
import QtQuick.Controls 2.0
Rectangle {
color: "green"
anchors.fill: parent
Text {
text: qsTr("Start\ndialog!")
font.pointSize: 30
}
}
5 changes: 5 additions & 0 deletions examples/standalone/start_dialog/start_dialog.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="start_dialog/">
<file>start_dialog.qml</file>
</qresource>
</RCC>
8 changes: 7 additions & 1 deletion include/ignition/gui/Application.hh
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ namespace ignition
/// plugins
kMainWindow = 0,

/// \brief One independent dialog per plugin
/// \brief One independent dialog per plugin. Also useful to open a
/// startup dialog before the main window.
kDialog = 1
};

Expand Down Expand Up @@ -169,6 +170,11 @@ namespace ignition
/// \brief Callback when user requests to close a plugin
public slots: void OnPluginClose();

/// \brief Create a main window. Just calls InitializeMainWindow.
/// \return True if successful
/// \sa InitializeMainWindow
public: bool CreateMainWindow();

/// \brief Create a main window, populate with previously loaded plugins
/// and apply previously loaded configuration.
/// An empty window will be created if no plugins have been loaded.
Expand Down
6 changes: 6 additions & 0 deletions src/Application.cc
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,12 @@ std::shared_ptr<Plugin> Application::PluginByName(
return nullptr;
}

/////////////////////////////////////////////////
bool Application::CreateMainWindow()
{
return this->InitializeMainWindow();
}

/////////////////////////////////////////////////
bool Application::InitializeMainWindow()
{
Expand Down
13 changes: 13 additions & 0 deletions src/Application_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,19 @@ TEST(ApplicationTest, IGN_UTILS_TEST_DISABLED_ON_WIN32(InitializeMainWindow))
// Show window
app.exec();
}

// Start in dialog mode and create main window later
{
Application app(g_argc, g_argv, WindowType::kDialog);

auto win = App()->findChild<MainWindow *>();
EXPECT_EQ(nullptr, win);

app.CreateMainWindow();

win = App()->findChild<MainWindow *>();
ASSERT_NE(nullptr, win);
}
}

//////////////////////////////////////////////////
Expand Down

0 comments on commit 27afc95

Please sign in to comment.