Skip to content

Commit

Permalink
Console
Browse files Browse the repository at this point in the history
  • Loading branch information
sguionni committed Jun 27, 2024
1 parent 4aa961f commit 92233a7
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 30 deletions.
2 changes: 2 additions & 0 deletions lib/app/include/app/args.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace VTX::App
return std::find( _args.begin(), _args.end(), p_arg ) != _args.end();
}

inline void add( const std::string & p_arg ) { _args.push_back( p_arg ); }

private:
std::vector<std::string> _args;
};
Expand Down
97 changes: 96 additions & 1 deletion lib/ui/qt/include/qt/dock_widget/console.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
#define __VTX_UI_QT_DOCK_WIDGET_CONSOLE__

#include <QDockWidget>
#include <QListWidget>
#include <QMenu>
#include <QVBoxLayout>
#include <app/vtx_app.hpp>
#include <mutex>
#include <util/enum.hpp>
#include <util/logger.hpp>

namespace VTX::UI::QT::DockWidget
{
Expand All @@ -13,13 +20,101 @@ namespace VTX::UI::QT::DockWidget
{
setAllowedAreas( Qt::BottomDockWidgetArea | Qt::TopDockWidgetArea );

QWidget * const mainWidget = new QWidget( this );
_listWidget = new QListWidget( this );
_listWidget->setContextMenuPolicy( Qt::ContextMenuPolicy::CustomContextMenu );

// Set widget.
setWidget( new QWidget( this ) );
QVBoxLayout * const mainLayout = new QVBoxLayout( mainWidget );
mainLayout->setContentsMargins( 0, 0, 0, 0 );
mainLayout->addWidget( _listWidget );
setWidget( mainWidget );

connect(
_listWidget,
&QListWidget::customContextMenuRequested,
this,
[ & ]( const QPoint & p_pos )
{
QMenu menu( this );
QAction * const clearAction = new QAction( "Clear", &menu );
connect( clearAction, &QAction::triggered, this, &Console::clear );
menu.addAction( clearAction );
menu.exec( _listWidget->mapToGlobal( p_pos ) );
}
);

LOGGER().onPrintLog += [ this ]( const Util::LogInfo & p_logInfo ) { _appendLog( p_logInfo ); };
}

virtual ~Console() {}

void clear()
{
_listWidgetMutex.lock();
_listWidget->clear();
_listWidgetMutex.unlock();
}

private:
const int _LOG_COUNT = 50;

QListWidget * _listWidget = nullptr;
std::mutex _listWidgetMutex = std::mutex();

void _appendLog( const Util::LogInfo & p_logInfo )
{
const std::string message = fmt::format( "[{}] {}", p_logInfo.date, p_logInfo.message );

QListWidgetItem * const newItem = new QListWidgetItem( QString::fromStdString( message ) );
newItem->setData( Qt::ForegroundRole, _getMessageColor( p_logInfo.level ) );
newItem->setFlags( Qt::ItemFlag::ItemNeverHasChildren );

_listWidgetMutex.lock();
_listWidget->addItem( newItem );
_listWidgetMutex.unlock();

if ( _listWidget->count() > _LOG_COUNT )
_flush();

// _appendLog can be called from a different thread
// Qt events are not thread safe and need to be called from the main thread
// We delayed the scrollToBottom on main thread at the end of frame.
APP().onEndOfFrameOneShot += [ this ]()
{
_listWidgetMutex.lock();
_listWidget->scrollToBottom();
_listWidgetMutex.unlock();
};
}

void _flush()
{
QListWidgetItem * const itemToRemove = _listWidget->takeItem( 0 );
_listWidgetMutex.lock();
_listWidget->removeItemWidget( itemToRemove );
_listWidgetMutex.unlock();
delete itemToRemove;
}

QColor _getMessageColor( const Util::LOG_LEVEL p_level )
{
QColor res;

/*
switch ( p_level )
{
case Util::LOG_LEVEL::LOG_DEBUG: res = CONSOLE_DEBUG_COLOR; break;
case Util::LOG_LEVEL::LOG_INFO: res = CONSOLE_INFO_COLOR; break;
case Util::LOG_LEVEL::LOG_WARNING: res = CONSOLE_WARNING_COLOR; break;
case Util::LOG_LEVEL::LOG_ERROR: res = CONSOLE_ERROR_COLOR; break;
case Util::LOG_LEVEL::LOG_CRITICAL: res = CONSOLE_CRITICAL_COLOR; break;
default: res = QColor(); break;
}
*/

return res;
}
};

} // namespace VTX::UI::QT::DockWidget
Expand Down
28 changes: 5 additions & 23 deletions lib/ui/qt/include/qt/main_window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "dock_widget/console.hpp"
#include "menu/file.hpp"
#include "menu/help.hpp"
#include "menu/view.hpp"
#include "tool_bar/camera.hpp"
#include <QDockWidget>
Expand Down Expand Up @@ -34,32 +35,13 @@ namespace VTX::UI::QT
void build()
{
// Main menu.
// File.
_createMenu<Menu::File>();

// Edit.
QMenu * editMenu = menuBar()->addMenu( "Edit" );

// View.
_createMenu<Menu::View>();

// Help.
QMenu * helpMenu = menuBar()->addMenu( "Help" );
helpMenu->addAction( new QAction( "Documentation" ) );
helpMenu->addAction( new QAction( "Report a bug" ) );
helpMenu->addAction( new QAction( "Check for updates" ) );
helpMenu->addAction( new QAction( "About" ) );
_createMenu<Menu::View>();
_createMenu<Menu::Help>();

// Toolbars.
// Camera.
QToolBar * visuToolBar = addToolBar( "Camera" );
visuToolBar->addAction( new QAction( "Perspective" ) );
visuToolBar->addSeparator();
visuToolBar->addAction( new QAction( "Trackball" ) );
visuToolBar->addAction( new QAction( "Freefly" ) );
visuToolBar->addSeparator();
visuToolBar->addAction( new QAction( "Orient" ) );
visuToolBar->addAction( new QAction( "Reset" ) );
_createToolBar<ToolBar::Camera>();

// Snapshot.
QToolBar * editToolBar = addToolBar( "Snapshot" );
Expand Down Expand Up @@ -161,7 +143,7 @@ namespace VTX::UI::QT
template<typename TB>
void _createToolBar()
{
menuBar()->addToolBar( new TB( this ) );
addToolBar( new TB( this ) );
}

template<typename DW>
Expand Down
26 changes: 26 additions & 0 deletions lib/ui/qt/include/qt/menu/help.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef __VTX_UI_QT_MENU_HELP__
#define __VTX_UI_QT_MENU_HELP__

#include <QMenu>

namespace VTX::UI::QT::Menu
{

class Help : public QMenu
{
public:
Help( QWidget * p_parent ) : QMenu( "Help", p_parent )
{
addAction( new QAction( "Documentation" ) );
addAction( new QAction( "Report a bug" ) );
addAction( new QAction( "Check for updates" ) );
addAction( new QAction( "About" ) );
}
virtual ~Help() {}

private:
};

} // namespace VTX::UI::QT::Menu

#endif
4 changes: 3 additions & 1 deletion lib/ui/qt/include/qt/resources.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
namespace VTX::UI::QT::Resources
{
constexpr QStringView APPLICATION_DISPLAY_NAME = u"VTX";
constexpr QStringView APPLICATION_NAME = u"VTX : High performance molecular visualization software";
constexpr QStringView APPLICATION_NAME = u"VTX";
constexpr QStringView ORGANIZATION_NAME = u"VTX Consortium";
constexpr QStringView ORGANIZATION_DOMAIN = u"vtx.drugdesign.fr";
// TODO: use App constants.
constexpr QStringView APPLICATION_VERSION = u"1.0";

Expand Down
11 changes: 10 additions & 1 deletion lib/ui/qt/include/qt/tool_bar/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@ namespace VTX::UI::QT::ToolBar
class Camera : public QToolBar
{
public:
Camera( QWidget * p_parent ) : QToolBar( "Camera", p_parent ) {}
Camera( QWidget * p_parent ) : QToolBar( "Camera", p_parent ) {
addAction( new QAction( "Perspective" ) );
addSeparator();
addAction( new QAction( "Trackball" ) );
addAction( new QAction( "Freefly" ) );
addSeparator();
addAction( new QAction( "Orient" ) );
addAction( new QAction( "Reset" ) );
}

virtual ~Camera() {}

private:
Expand Down
18 changes: 17 additions & 1 deletion lib/ui/qt/src/qt/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <QApplication>
#include <QFile>
#include <QIcon>
#include <QSettings>

namespace VTX::UI::QT
{
Expand All @@ -26,6 +27,8 @@ namespace VTX::UI::QT
_qApplication->setApplicationDisplayName( APPLICATION_DISPLAY_NAME.toString() );
_qApplication->setApplicationName( APPLICATION_NAME.toString() );
_qApplication->setApplicationVersion( APPLICATION_VERSION.toString() );
_qApplication->setOrganizationName( ORGANIZATION_NAME.toString() );
_qApplication->setOrganizationDomain( ORGANIZATION_DOMAIN.toString() );

_loadTheme();

Expand All @@ -44,11 +47,24 @@ namespace VTX::UI::QT
addMenuAction( MenuAction { "File", "MENU HOOK TEST" } );
addToolBarAction( ToolBarAction { "Camera", "TOOL BAR HOOK TEST" } );

// Restore settings.
QSettings settings;
_mainWindow->restoreGeometry( settings.value( "geometry" ).toByteArray() );
_mainWindow->restoreState( settings.value( "windowState" ).toByteArray() );

// Show app.
_mainWindow->show();
_qApplication->exec();
}

void Application::_stop() {}
void Application::_stop()
{
// Save settings.
QSettings settings;
settings.setValue( "geometry", _mainWindow->saveGeometry() );
settings.setValue( "windowState", _mainWindow->saveState() );
settings.sync();
}

void Application::_loadTheme()
{
Expand Down
6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ int main( int p_argc, char * p_argv[] )
app = std::unique_ptr<App::VTXApp>( &APP() );
#endif

app->start( args );

// std::unique_ptr<VTX::UI::Core::BaseUIApplication> vtxApplication = UI::UIGenerator::createUI();
// VTX::UI::Environment::get().setUIApp( vtxApplication.get() );
// vtxApplication->init();
Expand All @@ -68,7 +66,9 @@ int main( int p_argc, char * p_argv[] )
// vtxApplication->start( std::vector( args.begin() + 1, args.end() ) );

const FilePath molPath = IO::Internal::Filesystem::getInternalDataDir() / "md_0_1.gro";
// vtxApplication->start( { molPath.string() } );
// args.add( molPath.string() );

app->start( args );

LOGGER().stop();
return 0;
Expand Down

0 comments on commit 92233a7

Please sign in to comment.