1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <wx/bitmap.h>
#include <wx/filename.h>
#include <wx/log.h>
#include <wx/mstream.h>
#include <wx/stdpaths.h>
#include <advanced_config.h>
#include <asset_archive.h>
#include <bitmaps.h>
#include <bitmap_store.h>
#include <bitmaps/bitmap_info.h>
#include <hash.h>
#include <kiplatform/ui.h>
#include <paths.h>
#include <pgm_base.h>
#include <settings/common_settings.h>
/// A question-mark icon shown when we can't find a given bitmap in the archive
static const unsigned char s_imageNotFound[] = {
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x18, 0x08, 0x04, 0x00, 0x00, 0x00, 0x4a, 0x7e, 0xf5,
0x73, 0x00, 0x00, 0x01, 0xab, 0x49, 0x44, 0x41, 0x54, 0x38, 0xcb, 0x8d, 0xd4, 0x3f, 0x2c, 0x03,
0x51, 0x18, 0x00, 0xf0, 0x4b, 0x0c, 0x22, 0xb1, 0x19, 0x8c, 0x46, 0x09, 0x62, 0x60, 0x68, 0x62,
0x20, 0xb1, 0x98, 0x24, 0x24, 0x77, 0x08, 0xd2, 0x10, 0x03, 0x06, 0x8b, 0x90, 0x98, 0x48, 0x25,
0x3a, 0x34, 0x12, 0xb1, 0x19, 0x0c, 0xa2, 0x34, 0xfe, 0xc4, 0x24, 0x29, 0x93, 0xa1, 0x0c, 0x52,
0x7f, 0xe2, 0xcf, 0x20, 0x5a, 0x83, 0xa1, 0xb4, 0xf7, 0xa7, 0x77, 0xef, 0xde, 0xdd, 0xd3, 0xde,
0xd1, 0x7e, 0x7a, 0x17, 0xda, 0xe2, 0xb5, 0xee, 0x7d, 0xdb, 0x7d, 0xdf, 0xef, 0xde, 0xbb, 0xfb,
0xde, 0x7b, 0x0c, 0x0b, 0x6c, 0xfa, 0x77, 0x70, 0x46, 0x9f, 0x69, 0x05, 0x67, 0xfe, 0xc9, 0x01,
0xc3, 0xa6, 0x99, 0xa2, 0xa1, 0xb5, 0x8b, 0x81, 0xa4, 0x9a, 0x20, 0xf7, 0xca, 0x39, 0x0a, 0x2b,
0x11, 0x84, 0x0c, 0xe1, 0x20, 0x55, 0x57, 0xc8, 0xe7, 0xaa, 0x0b, 0x80, 0xb4, 0xf0, 0x77, 0xcf,
0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 0x77, 0x8e, 0x0a, 0xd4, 0x85, 0xbd, 0xac,
0x3b, 0xb7, 0x8b, 0x2e, 0x54, 0xdc, 0xed, 0x08, 0xe0, 0x39, 0xf9, 0xcd, 0x0a, 0x45, 0xd7, 0x3a,
0x1d, 0x81, 0x72, 0xa3, 0x04, 0x50, 0x5d, 0xc8, 0x2b, 0xfb, 0xb4, 0x0e, 0x87, 0x40, 0x19, 0x7d,
0xd5, 0xfd, 0x99, 0x8d, 0x6c, 0x4c, 0x4b, 0x4e, 0x3b, 0x02, 0x7c, 0x6c, 0xc6, 0xee, 0xf0, 0x64,
0xee, 0x1c, 0x38, 0x02, 0x32, 0x71, 0xdb, 0xa0, 0x1f, 0xd4, 0x8f, 0xc2, 0xce, 0x2d, 0x37, 0xc3,
0xd5, 0x8a, 0x7d, 0x0a, 0x96, 0x80, 0x7f, 0x72, 0x34, 0x83, 0xd6, 0x28, 0xf1, 0x4f, 0x28, 0x82,
0x24, 0x59, 0x75, 0xd1, 0x00, 0xe5, 0x9a, 0xb1, 0x2e, 0x1a, 0xce, 0xa0, 0x3d, 0x67, 0xe1, 0x13,
0xb5, 0xc5, 0x7e, 0xb7, 0x69, 0x08, 0x53, 0x5c, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44,
0xae, 0x42, 0x60, 0x82,
};
/// Icon used for EDA_ITEMs that don't have a custom icon configured
/// @todo Replace this with an external file?
static const unsigned char s_dummyItem[] = {
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x10, 0x08, 0x06, 0x00, 0x00, 0x00, 0x1f, 0xf3, 0xff,
0x61, 0x00, 0x00, 0x00, 0x5f, 0x49, 0x44, 0x41, 0x54, 0x38, 0xcb, 0x63, 0xf8, 0xff, 0xff, 0x3f,
0x03, 0x25, 0x98, 0x61, 0x68, 0x1a, 0x00, 0x04, 0x46, 0x40, 0xfc, 0x02, 0x88, 0x45, 0x41, 0x1c,
0x76, 0x20, 0xfe, 0x01, 0xc4, 0xbe, 0x24, 0x18, 0x60, 0x01, 0xc4, 0x20, 0x86, 0x04, 0x88, 0xc3,
0x01, 0xe5, 0x04, 0x0c, 0xb8, 0x01, 0x37, 0x81, 0xf8, 0x04, 0x91, 0xf8, 0x0a, 0x54, 0x8f, 0x06,
0xb2, 0x01, 0x9b, 0x81, 0x78, 0x02, 0x91, 0x78, 0x05, 0x54, 0x8f, 0xca, 0xe0, 0x08, 0x03, 0x36,
0xa8, 0xbf, 0xec, 0xc8, 0x32, 0x80, 0xcc, 0x84, 0x04, 0x0a, 0xbc, 0x1d, 0x40, 0x2c, 0xc8, 0x30,
0xf4, 0x33, 0x13, 0x00, 0x6b, 0x1a, 0x46, 0x7b, 0x68, 0xe7, 0x0f, 0x0b, 0x00, 0x00, 0x00, 0x00,
0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82,
};
static const wxString traceBitmaps = wxT( "KICAD_BITMAPS" );
static const wxString IMAGE_ARCHIVE = wxT( "images.tar.gz" );
size_t std::hash<std::pair<BITMAPS, int>>::operator()( const std::pair<BITMAPS, int>& aPair ) const
{
std::size_t seed = 0xa82de1c0;
hash_combine( seed, static_cast<size_t>( aPair.first ), static_cast<size_t>( aPair.second ) );
return seed;
}
BITMAP_STORE::BITMAP_STORE()
{
wxFileName path( PATHS::GetStockDataPath() + wxT( "/resources" ), IMAGE_ARCHIVE );
wxLogTrace( traceBitmaps, "Loading bitmaps from " + path.GetFullPath() );
m_archive = std::make_unique<ASSET_ARCHIVE>( path.GetFullPath() );
buildBitmapInfoCache();
ThemeChanged();
}
wxBitmap BITMAP_STORE::GetBitmap( BITMAPS aBitmapId, int aHeight )
{
return wxBitmap( getImage( aBitmapId, aHeight ) );
}
wxBitmapBundle BITMAP_STORE::GetBitmapBundle( BITMAPS aBitmapId, int aMinHeight )
{
wxVector<wxBitmap> bmps;
for( const BITMAP_INFO& info : m_bitmapInfoCache[aBitmapId] )
{
if( info.theme != m_theme )
continue;
if( aMinHeight > 0 && info.height < aMinHeight )
continue;
bmps.push_back( wxBitmap( getImage( info.id, info.height ) ) );
}
return wxBitmapBundle::FromBitmaps( bmps );
}
wxBitmapBundle BITMAP_STORE::GetDisabledBitmapBundle( BITMAPS aBitmapId )
{
wxVector<wxBitmap> bmps;
for( const BITMAP_INFO& info : m_bitmapInfoCache[aBitmapId] )
{
if( info.theme != m_theme )
continue;
wxBitmap bmp( getImage( info.id, info.height )
.ConvertToDisabled( KIPLATFORM::UI::IsDarkTheme() ? 70 : 255 ) );
bmps.push_back( bmp );
}
return wxBitmapBundle::FromBitmaps( bmps );
}
wxBitmap BITMAP_STORE::GetBitmapScaled( BITMAPS aBitmapId, int aScaleFactor, int aHeight )
{
wxImage image = getImage( aBitmapId, aHeight );
// Bilinear seems to genuinely look better for these line-drawing icons
// than bicubic, despite claims in the wx documentation that bicubic is
// "highest quality". I don't recommend changing this. Bicubic looks
// blurry and makes me want an eye exam.
image.Rescale( aScaleFactor * image.GetWidth() / 4, aScaleFactor * image.GetHeight() / 4,
wxIMAGE_QUALITY_BILINEAR );
return wxBitmap( image );
}
wxImage BITMAP_STORE::getImage( BITMAPS aBitmapId, int aHeight )
{
const unsigned char* data = nullptr;
long count;
if( aBitmapId == BITMAPS::dummy_item )
{
data = s_dummyItem;
count = sizeof( s_dummyItem );
}
else
{
count = m_archive->GetFilePointer( bitmapName( aBitmapId, aHeight ), &data );
if( count < 0 )
{
wxLogTrace( traceBitmaps,
"Bitmap for %d, %d, %s has an info tag with file %s,"
"but that file could not be found in the archive!",
aBitmapId, aHeight, m_theme );
data = s_imageNotFound;
count = sizeof( s_imageNotFound );
}
}
wxMemoryInputStream is( data, count );
wxImage image( is, wxBITMAP_TYPE_PNG );
return image;
}
void BITMAP_STORE::ThemeChanged()
{
COMMON_SETTINGS* settings = Pgm().GetCommonSettings();
wxString oldTheme = m_theme;
if( settings )
{
switch( settings->m_Appearance.icon_theme )
{
case ICON_THEME::LIGHT: m_theme = wxT( "light" ); break;
case ICON_THEME::DARK: m_theme = wxT( "dark" ); break;
case ICON_THEME::AUTO:
m_theme = KIPLATFORM::UI::IsDarkTheme() ? wxT( "dark" ) : wxT( "light" );
break;
}
}
else
{
m_theme = KIPLATFORM::UI::IsDarkTheme() ? wxT( "dark" ) : wxT( "light" );
}
if( !oldTheme.IsSameAs( m_theme ) )
m_bitmapNameCache.clear();
}
const wxString& BITMAP_STORE::bitmapName( BITMAPS aBitmapId, int aHeight )
{
std::pair<BITMAPS, int> key = std::make_pair( aBitmapId, aHeight );
if( !m_bitmapNameCache.count( key ) )
m_bitmapNameCache[key] = computeBitmapName( aBitmapId, aHeight );
return m_bitmapNameCache.at( key );
}
wxString BITMAP_STORE::computeBitmapName( BITMAPS aBitmapId, int aHeight )
{
if( !m_bitmapInfoCache.count( aBitmapId ) )
{
wxLogTrace( traceBitmaps, "No bitmap info available for %d", aBitmapId );
return wxEmptyString;
}
wxString fn;
for( const BITMAP_INFO& info : m_bitmapInfoCache.at( aBitmapId ) )
{
if( info.theme != m_theme )
continue;
if( aHeight < 0 || info.height == aHeight )
{
fn = info.filename;
break;
}
}
if( fn.IsEmpty() )
{
wxLogTrace( traceBitmaps, "No bitmap found matching ID %d, height %d, theme %s", aBitmapId,
aHeight, m_theme );
return m_bitmapInfoCache.at( aBitmapId ).begin()->filename;
}
return fn;
}
void BITMAP_STORE::buildBitmapInfoCache()
{
BuildBitmapInfo( m_bitmapInfoCache );
}
|