From 2f56a5eee4d42c384f1435d762352be9f4c18977 Mon Sep 17 00:00:00 2001 From: dfighter1985 Date: Wed, 6 Aug 2014 13:25:50 +0200 Subject: [PATCH] Statically load the empty tile image so that we don't need to have over 9000 instances of it. Also display an error message if loading fails on startup. --- .../tile_editor/tile_editor_main_window.cpp | 14 ++++++++ .../tile_editor/tile_editor_main_window.h | 2 ++ .../src/plugins/tile_editor/tile_item.cpp | 36 ++++++++++++++----- .../src/plugins/tile_editor/tile_item.h | 1 + 4 files changed, 45 insertions(+), 8 deletions(-) diff --git a/code/studio/src/plugins/tile_editor/tile_editor_main_window.cpp b/code/studio/src/plugins/tile_editor/tile_editor_main_window.cpp index 9f4851058..90f4c7131 100644 --- a/code/studio/src/plugins/tile_editor/tile_editor_main_window.cpp +++ b/code/studio/src/plugins/tile_editor/tile_editor_main_window.cpp @@ -37,6 +37,8 @@ #include "land_edit_dialog.h" +#include + TileEditorMainWindow::TileEditorMainWindow(QWidget *parent) : QMainWindow(parent), m_ui(new Ui::TileEditorMainWindow) @@ -44,6 +46,11 @@ TileEditorMainWindow::TileEditorMainWindow(QWidget *parent) m_ui->setupUi(this); m_undoStack = new QUndoStack(this); + if( !TileItemNode::loadEmptyPixmap() ) + { + QTimer::singleShot( 0, this, SLOT( onEmptyImageLoadFailed() ) ); + } + // Retrieve the menu manager Core::ICore *core = Core::ICore::instance(); Core::MenuManager *menuManager = core->menuManager(); @@ -341,6 +348,13 @@ void TileEditorMainWindow::onRotate( int id ) m_tileModel->setAlphaRot( id ); } +void TileEditorMainWindow::onEmptyImageLoadFailed() +{ + QMessageBox::critical( this, + tr( "Empty image load failed" ), + tr( "Couldn't load the image for empty tiles :(" ) ); +} + void TileEditorMainWindow::onActionAddTile(bool triggered) { onActionAddTile(m_ui->tileViewTabWidget->currentIndex()); diff --git a/code/studio/src/plugins/tile_editor/tile_editor_main_window.h b/code/studio/src/plugins/tile_editor/tile_editor_main_window.h index 9dee6954d..d06d6deca 100644 --- a/code/studio/src/plugins/tile_editor/tile_editor_main_window.h +++ b/code/studio/src/plugins/tile_editor/tile_editor_main_window.h @@ -80,6 +80,8 @@ private Q_SLOTS: void onZoomFactor(int level); void onRotate(int id); + void onEmptyImageLoadFailed(); + private: void onActionAddTile(int tabId); void onActionDeleteTile(int tabId); diff --git a/code/studio/src/plugins/tile_editor/tile_item.cpp b/code/studio/src/plugins/tile_editor/tile_item.cpp index 311b711cc..71fe336a2 100644 --- a/code/studio/src/plugins/tile_editor/tile_item.cpp +++ b/code/studio/src/plugins/tile_editor/tile_item.cpp @@ -271,6 +271,12 @@ public: bool loadImage( TileConstants::TTileChannel channel, const QString &fn ) { + if( fn.isEmpty() ) + { + pixmaps[ channel ] = TileItemNodePvt::emptyPm(); + return true; + } + QPixmap temp; bool b = temp.load( fn ); @@ -284,6 +290,17 @@ public: return true; } + static bool loadEmptyImage() + { + bool b = empty.load( ":/placeHolder/images/empty_image.png" ); + if( !b ) + { + empty = QPixmap(); + } + + return b; + } + void clearImage( TileConstants::TTileChannel channel ) { pixmaps[ channel ] = QPixmap(); @@ -293,10 +310,15 @@ public: return pixmaps[ channel ]; } + static QPixmap& emptyPm(){ return empty; } + private: QPixmap pixmaps[ TileConstants::TileChannelCount ]; + static QPixmap empty; + }; +QPixmap TileItemNodePvt::empty = QPixmap(); TileConstants::TTileChannel TileItemNode::s_displayChannel = TileConstants::TileDiffuse; TileItemNode::TileItemNode( TileConstants::TNodeTileType type, int tileId, Node *parent ) @@ -321,14 +343,7 @@ TileItemNode::~TileItemNode() bool TileItemNode::setTileFilename(TileConstants::TTileChannel channel, QString filename) { - QString fn = filename; - - if( filename.isEmpty() || ( filename == "empty" ) ) - { - fn = ":/placeHolder/images/empty_image.png"; - } - - bool b = pvt->loadImage( channel, fn ); + bool b = pvt->loadImage( channel, filename ); if( !b ) return false; @@ -346,6 +361,11 @@ QString TileItemNode::getTileFilename(TileConstants::TTileChannel channel) return itr.value(); } +bool TileItemNode::loadEmptyPixmap() +{ + return TileItemNodePvt::loadEmptyImage(); +} + QVariant TileItemNode::data(int column, int role) const { QString tileFilename = m_tileFilename[ TileItemNode::s_displayChannel ]; diff --git a/code/studio/src/plugins/tile_editor/tile_item.h b/code/studio/src/plugins/tile_editor/tile_item.h index 1af7833dd..5387a710d 100644 --- a/code/studio/src/plugins/tile_editor/tile_item.h +++ b/code/studio/src/plugins/tile_editor/tile_item.h @@ -117,6 +117,7 @@ public: static void setDisplayChannel( TileConstants::TTileChannel channel ){ s_displayChannel = channel; } static TileConstants::TTileChannel displayChannel(){ return s_displayChannel; } + static bool loadEmptyPixmap(); QVariant pixmap( TileConstants::TTileChannel channel ) const;