Linux multi-monitor fullscreen support
[ryzomcore.git] / studio / src / plugins / gui_editor / texture_chooser.cpp
blob406f259165b607bd68400c4444cd4d3bc8c2c4d1
1 // Ryzom Core Studio GUI Editor plugin <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2014 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "texture_chooser.h"
21 #include "nel/misc/path.h"
22 #include "nel/misc/bitmap.h"
23 #include "nel/misc/file.h"
24 #include <vector>
25 #include <string>
26 #include <QPixmap>
27 #include <QImage>
28 #include <QListWidget>
30 #include "nel/gui/view_renderer.h"
32 struct TextureChooserPrivate
34 QListWidget *fileTextures;
35 QListWidget *atlasTextures;
37 TextureChooserPrivate()
39 fileTextures = new QListWidget();
40 atlasTextures = new QListWidget();
44 TextureChooser::TextureChooser( QDialog *parent ) :
45 QDialog( parent )
47 setupUi( this );
49 d_ptr = new TextureChooserPrivate;
50 this->tabWidget->clear();
51 this->tabWidget->addTab( d_ptr->fileTextures, tr( "File textures" ) );
52 this->tabWidget->addTab( d_ptr->atlasTextures, tr( "Atlas texture" ) );
54 setupConnections();
57 TextureChooser::~TextureChooser()
59 delete d_ptr;
60 d_ptr = NULL;
64 void TextureChooser::load()
66 // Load the file textures
67 d_ptr->fileTextures->clear();
69 std::vector< std::string > textures;
70 //NLMISC::CPath::getFileList( "tga", textures );
71 NLMISC::CPath::getFileListByPath( "dds", "interfaces", textures );
72 NLMISC::CPath::getFileListByPath( "dds", "gamedev", textures );
74 std::sort( textures.begin(), textures.end() );
76 std::vector< std::string >::const_iterator itr = textures.begin();
77 while( itr != textures.end() )
79 d_ptr->fileTextures->addItem( itr->c_str() );
80 ++itr;
83 // Now load the atlas textures
84 d_ptr->atlasTextures->clear();
85 textures.clear();
87 NLGUI::CViewRenderer::getInstance()->getTextureNames( textures );
88 itr = textures.begin();
89 while( itr != textures.end() )
91 d_ptr->atlasTextures->addItem( itr->c_str() );
92 ++itr;
95 // set the file textures row after the atlas, because they are shown first
96 d_ptr->atlasTextures->setCurrentRow( 0 );
97 d_ptr->fileTextures->setCurrentRow( 0 );
100 void TextureChooser::accept()
102 QListWidgetItem *item = d_ptr->fileTextures->currentItem();
103 if( item == NULL )
104 return;
106 selection = item->text();
107 QDialog::accept();
110 void TextureChooser::reject()
112 selection = "";
114 QDialog::reject();
117 void TextureChooser::onFileTxtRowChanged( int row )
119 if( row < 0 )
120 return;
122 QListWidgetItem *item = d_ptr->fileTextures->item( row );
123 QString fn = item->text();
125 std::string rfn = fn.toUtf8().constData();
126 rfn = NLMISC::CPath::lookup( rfn );
128 NLMISC::CIFile f;
129 bool b = f.open( rfn );
130 if( !b )
132 return;
135 NLMISC::CBitmap bm;
136 uint8 depth = bm.load( f );
137 f.close();
138 b = bm.convertToType( NLMISC::CBitmap::RGBA );
139 if( !b )
141 return;
144 setPreviewImage( bm );
147 void TextureChooser::onAtlasTxtRowChanged( int row )
149 if( row < 0 )
150 return;
152 QListWidgetItem *item = d_ptr->atlasTextures->item( row );
153 QString fn = item->text();
155 std::string rfn = fn.toUtf8().constData();
157 NLMISC::CBitmap bm;
159 bool b = NLGUI::CViewRenderer::getInstance()->getTexture( bm, rfn );
160 if( !b )
161 return;
163 setPreviewImage( bm );
167 void TextureChooser::setupConnections()
169 connect( d_ptr->fileTextures, SIGNAL( currentRowChanged( int ) ), this, SLOT( onFileTxtRowChanged( int ) ) );
170 connect( d_ptr->atlasTextures, SIGNAL( currentRowChanged( int ) ), this, SLOT( onAtlasTxtRowChanged( int ) ) );
173 void TextureChooser::setPreviewImage( NLMISC::CBitmap &bm )
175 // should be depth, but CBitmap always uses 32 bit to store the image
176 uint32 size = bm.getSize() * ( 32 / 8 );
177 uint8 *data = new uint8[ size ];
178 bm.getData( data );
180 /// Convert from ABGR to ARGB
182 int i = 0;
183 while( i < size )
185 uint8 t = 0;
187 /// ABGR
188 t = data[ i ];
189 data[ i ] = data[ i + 2 ];
190 data[ i + 2 ] = t;
192 i += 4;
196 QImage img( data, bm.getWidth(), bm.getHeight(), QImage::Format_ARGB32 );
197 label->setPixmap( QPixmap::fromImage( img ) );
199 delete data;
200 data = NULL;