From 71bab17233fea1ced67edbd9771b2cdee271fe7c Mon Sep 17 00:00:00 2001 From: Jonas Trappenberg Date: Sun, 18 Jan 2015 15:50:17 -0800 Subject: [PATCH] Work on codestyle and readability --- include/Plugin.h | 10 ++++--- include/lmms_basics.h | 6 ++-- src/gui/MainWindow.cpp | 56 +++++++++++++++++------------------- src/gui/PluginBrowser.cpp | 7 +---- src/gui/embed.cpp | 49 +++++++++++++++---------------- src/gui/widgets/SideBar.cpp | 57 +++++++++++++++++++------------------ 6 files changed, 89 insertions(+), 96 deletions(-) diff --git a/include/Plugin.h b/include/Plugin.h index b73238ab5..40a075c67 100644 --- a/include/Plugin.h +++ b/include/Plugin.h @@ -134,7 +134,7 @@ public: const Plugin::PluginTypes m_type; } ; - SubPluginFeatures * subPluginFeatures; + SubPluginFeatures *subPluginFeatures; } ; @@ -148,7 +148,9 @@ public: // returns display-name out of descriptor virtual QString displayName() const { - return Model::displayName().isEmpty() ? m_descriptor->displayName : Model::displayName(); + return Model::displayName().isEmpty() + ? m_descriptor->displayName + : Model::displayName(); } // return plugin-type @@ -173,13 +175,13 @@ public: // returns an instance of a plugin whose name matches to given one // if specified plugin couldn't be loaded, it creates a dummy-plugin - static Plugin * instantiate( const QString& pluginName, Model * parent, void * data ); + static Plugin * instantiate( const QString& pluginName, Model *parent, void * data ); // fills given list with descriptors of all available plugins static void getDescriptorsOfAvailPlugins( DescriptorList& pluginDescriptors ); // create a view for the model - PluginView * createView( QWidget* parent ); + PluginView* createView( QWidget* parent ); protected: diff --git a/include/lmms_basics.h b/include/lmms_basics.h index bdd72f4fa..082415124 100644 --- a/include/lmms_basics.h +++ b/include/lmms_basics.h @@ -97,13 +97,13 @@ inline float typeInfo::minEps() } template<> -inline bool typeInfo::isEqual( float _x, float _y ) +inline bool typeInfo::isEqual( float x, float y ) { - if( likely( _x == _y ) ) + if( unlikely( x == y ) ) { return true; } - return absVal( _x - _y ) < minEps(); + return absVal( x - y ) < minEps(); } diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 612a7409e..22de85bc4 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -94,24 +94,25 @@ MainWindow::MainWindow() : QSplitter * splitter = new QSplitter( Qt::Horizontal, w ); splitter->setChildrenCollapsible( false ); - QString wdir = ConfigManager::inst()->workingDir(); + ConfigManager* confMgr = ConfigManager::inst(); + QString wdir = confMgr->workingDir(); sideBar->appendTab( new PluginBrowser( splitter ) ); sideBar->appendTab( new FileBrowser( - ConfigManager::inst()->userProjectsDir() + "*" + - ConfigManager::inst()->factoryProjectsDir(), + confMgr->userProjectsDir() + "*" + + confMgr->factoryProjectsDir(), "*.mmp *.mmpz *.xml *.mid *.flp", tr( "My Projects" ), embed::getIconPixmap( "project_file" ).transformed( QTransform().rotate( 90 ) ), splitter, false, true ) ); sideBar->appendTab( new FileBrowser( - ConfigManager::inst()->userSamplesDir() + "*" + - ConfigManager::inst()->factorySamplesDir(), + confMgr->userSamplesDir() + "*" + + confMgr->factorySamplesDir(), "*", tr( "My Samples" ), embed::getIconPixmap( "sample_file" ).transformed( QTransform().rotate( 90 ) ), splitter, false, true ) ); sideBar->appendTab( new FileBrowser( - ConfigManager::inst()->userPresetsDir() + "*" + - ConfigManager::inst()->factoryPresetsDir(), + confMgr->userPresetsDir() + "*" + + confMgr->factoryPresetsDir(), "*.xpf *.cs.xml *.xiz", tr( "My Presets" ), embed::getIconPixmap( "preset_file" ).transformed( QTransform().rotate( 90 ) ), @@ -121,33 +122,30 @@ MainWindow::MainWindow() : embed::getIconPixmap( "home" ).transformed( QTransform().rotate( 90 ) ), splitter, false, true ) ); + QStringList root_paths; + QString title = tr( "Root directory" ); + bool dirs_as_items = false; + #ifdef LMMS_BUILD_APPLE + title = tr( "Volumes" ); root_paths += "/Volumes"; -#else +#elif defined(LMMS_BUILD_WIN32) + title = tr( "My Computer" ); + dirs_as_items = true; +#endif + +#if ! defined(LMMS_BUILD_APPLE) QFileInfoList drives = QDir::drives(); foreach( const QFileInfo & drive, drives ) { root_paths += drive.absolutePath(); } #endif - sideBar->appendTab( new FileBrowser( root_paths.join( "*" ), "*", -#ifdef LMMS_BUILD_WIN32 - tr( "My Computer" ), -#elif defined(LMMS_BUILD_APPLE) - tr( "Volumes" ), -#else - tr( "Root Directory" ), -#endif + sideBar->appendTab( new FileBrowser( root_paths.join( "*" ), "*", title, embed::getIconPixmap( "computer" ).transformed( QTransform().rotate( 90 ) ), - splitter, -#ifdef LMMS_BUILD_WIN32 - true -#else - false -#endif - ) ); + splitter, dirs_as_items) ); m_workspace = new QMdiArea( splitter ); @@ -190,13 +188,13 @@ MainWindow::MainWindow() : vbox->addWidget( w ); setCentralWidget( main_widget ); - m_updateTimer.start( 1000 / 20, this ); // 20 fps + m_updateTimer.start( 1000 / 20, this ); // 20 fps if( ConfigManager::inst()->value( "ui", "enableautosave" ).toInt() ) { // connect auto save connect(&m_autoSaveTimer, SIGNAL(timeout()), this, SLOT(autoSave())); - m_autoSaveTimer.start(1000 * 60); // 1 minute + m_autoSaveTimer.start(1000 * 60); // 1 minute } connect( Engine::getSong(), SIGNAL( playbackStateChanged() ), @@ -208,12 +206,10 @@ MainWindow::MainWindow() : MainWindow::~MainWindow() { - for( QList::iterator it = m_tools.begin(); - it != m_tools.end(); ++it ) + for( PluginView *view : m_tools ) { - Model * m = ( *it )->model(); - delete *it; - delete m; + delete view; + delete view->model(); } // TODO: Close tools // destroy engine which will do further cleanups etc. diff --git a/src/gui/PluginBrowser.cpp b/src/gui/PluginBrowser.cpp index 5c10dd368..96f33ae75 100644 --- a/src/gui/PluginBrowser.cpp +++ b/src/gui/PluginBrowser.cpp @@ -88,8 +88,6 @@ PluginBrowser::~PluginBrowser() - - PluginDescList::PluginDescList(QWidget *parent) : QWidget(parent) { @@ -98,7 +96,6 @@ PluginDescList::PluginDescList(QWidget *parent) : Plugin::getDescriptorsOfAvailPlugins( m_pluginDescriptors ); std::sort(m_pluginDescriptors.begin(), m_pluginDescriptors.end(), pluginBefore); - for( Plugin::DescriptorList::const_iterator it = m_pluginDescriptors.constBegin(); it != m_pluginDescriptors.constEnd(); ++it ) { @@ -117,8 +114,6 @@ PluginDescList::PluginDescList(QWidget *parent) : - - PluginDescWidget::PluginDescWidget( const Plugin::Descriptor & _pd, QWidget * _parent ) : QWidget( _parent ), @@ -181,7 +176,6 @@ void PluginDescWidget::paintEvent( QPaintEvent * ) m_targetHeight = qMax( 60, 25 + br.height() ); } } - } @@ -237,6 +231,7 @@ void PluginDescWidget::updateHeight() m_updateTimer.stop(); return; } + if( !m_updateTimer.isActive() ) { m_updateTimer.start( 15 ); diff --git a/src/gui/embed.cpp b/src/gui/embed.cpp index 08af0c0a2..4e6ecaa13 100644 --- a/src/gui/embed.cpp +++ b/src/gui/embed.cpp @@ -45,73 +45,70 @@ namespace #include "embedded_resources.h" -QPixmap getIconPixmap( const char * _name, int _w, int _h ) +QPixmap getIconPixmap( const char * pixmapName, int width, int height ) { - if( _w == -1 || _h == -1 ) + if( width == -1 || height == -1 ) { - // Return cached pixmap - QPixmap cached = s_pixmapCache.value( _name ); + // Return cached pixmap + QPixmap cached = s_pixmapCache.value( pixmapName ); if( !cached.isNull() ) { return cached; } // Or try to load it - QList formats = - QImageReader::supportedImageFormats(); + QList formats = QImageReader::supportedImageFormats(); QList candidates; - QPixmap p; + QPixmap pixmap; QString name; int i; - for ( i = 0; i < formats.size() && p.isNull(); ++i ) + for ( i = 0; i < formats.size() && pixmap.isNull(); ++i ) { - candidates << QString( _name ) + "." + formats.at( i ).data(); + candidates << QString( pixmapName ) + "." + formats.at( i ).data(); } #ifdef PLUGIN_NAME - for ( i = 0; i < candidates.size() && p.isNull(); ++i ) { + for ( i = 0; i < candidates.size() && pixmap.isNull(); ++i ) { name = candidates.at( i ); - p = QPixmap( ConfigManager::inst()->artworkDir() + "plugins/" + + pixmap = QPixmap( ConfigManager::inst()->artworkDir() + "plugins/" + STRINGIFY( PLUGIN_NAME ) + "_" + name ); } #endif - for ( i = 0; i < candidates.size() && p.isNull(); ++i ) { + for ( i = 0; i < candidates.size() && pixmap.isNull(); ++i ) { name = candidates.at( i ); - p = QPixmap( ConfigManager::inst()->artworkDir() + name ); + pixmap = QPixmap( ConfigManager::inst()->artworkDir() + name ); } // nothing found, so look in default-artwork-dir - for ( i = 0; i < candidates.size() && p.isNull(); ++i ) { + for ( i = 0; i < candidates.size() && pixmap.isNull(); ++i ) { name = candidates.at( i ); - p = QPixmap( ConfigManager::inst()->defaultArtworkDir() - + name ); + pixmap = QPixmap( ConfigManager::inst()->defaultArtworkDir() + name ); } - for ( i = 0; i < candidates.size() && p.isNull(); ++i ) { + for ( i = 0; i < candidates.size() && pixmap.isNull(); ++i ) { name = candidates.at( i ); const embed::descriptor & e = findEmbeddedData( name.toUtf8().constData() ); // found? - if( QString( e.name ) == name ) + if( name == e.name ) { - p.loadFromData( e.data, e.size ); + pixmap.loadFromData( e.data, e.size ); } } // Fallback - if(p.isNull()) + if( pixmap.isNull() ) { - p = QPixmap( 1, 1 ); + pixmap = QPixmap( 1, 1 ); } // Save to cache and return - s_pixmapCache.insert( _name, p ); - return p; - + s_pixmapCache.insert( pixmapName, pixmap ); + return pixmap; } - return getIconPixmap( _name ). - scaled( _w, _h, Qt::IgnoreAspectRatio, + return getIconPixmap( pixmapName ). + scaled( width, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation ); } diff --git a/src/gui/widgets/SideBar.cpp b/src/gui/widgets/SideBar.cpp index 409fbf771..990bae7f6 100644 --- a/src/gui/widgets/SideBar.cpp +++ b/src/gui/widgets/SideBar.cpp @@ -107,53 +107,56 @@ SideBar::~SideBar() -void SideBar::appendTab( SideBarWidget * _sbw ) +void SideBar::appendTab( SideBarWidget *widget ) { - SideBarButton * btn = new SideBarButton( orientation(), this ); - btn->setText( _sbw->title() ); - btn->setIcon( _sbw->icon() ); - btn->setCheckable( true ); - m_widgets[btn] = _sbw; - m_btnGroup.addButton( btn ); - addWidget( btn ); + SideBarButton *button = new SideBarButton( orientation(), this ); + button->setText( widget->title() ); + button->setIcon( widget->icon() ); + button->setCheckable( true ); + m_widgets[button] = widget; + m_btnGroup.addButton( button ); + addWidget( button ); - _sbw->hide(); - _sbw->setMinimumWidth( 200 ); + widget->hide(); + widget->setMinimumWidth( 200 ); - ToolTip::add( btn, _sbw->title() ); + ToolTip::add( button, widget->title() ); } -void SideBar::toggleButton( QAbstractButton * _btn ) +void SideBar::toggleButton( QAbstractButton * button ) { - QToolButton * toolButton = NULL; - QWidget * activeWidget = NULL; - for( ButtonMap::Iterator it = m_widgets.begin(); - it != m_widgets.end(); ++it ) + QToolButton *toolButton = NULL; + QWidget *activeWidget = NULL; + + for( auto it = m_widgets.begin(); it != m_widgets.end(); ++it ) { - QToolButton * curBtn = it.key(); - if( curBtn != _btn ) + QToolButton *curBtn = it.key(); + QWidget *curWidget = it.value(); + + if( curBtn == button ) + { + toolButton = curBtn; + activeWidget = curWidget; + } + else { curBtn->setChecked( false ); curBtn->setToolButtonStyle( Qt::ToolButtonIconOnly ); } - else + + if( curWidget ) { - toolButton = it.key(); - activeWidget = it.value(); - } - if( it.value() ) - { - it.value()->hide(); + curWidget->hide(); } } if( toolButton && activeWidget ) { - activeWidget->setVisible( _btn->isChecked() ); - toolButton->setToolButtonStyle( _btn->isChecked() ? + activeWidget->setVisible( button->isChecked() ); + toolButton->setToolButtonStyle( button->isChecked() ? Qt::ToolButtonTextBesideIcon : Qt::ToolButtonIconOnly ); } }