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
|
/*
xmunipack - FITS extension side-list
Copyright © 2019-22 F.Hroch (hroch@physics.muni.cz)
This file is part of Munipack.
Munipack 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.
Munipack 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 Munipack. If not, see <http://www.gnu.org/licenses/>.
*/
#include "xmunipack.h"
#include <wx/dataview.h>
#include <wx/wx.h>
#include <wx/graphics.h>
#include <vector>
#define LABEL "Extension"
using namespace std;
MuniExtensionList::MuniExtensionList(wxWindow *w, const MuniConfig *c):
wxDataViewListCtrl(w,wxID_ANY), config(c)
{
// indexes
idxtype[HDU_UNKNOWN] = 0;
idxtype[HDU_HEAD] = 1;
idxtype[HDU_IMAGE] = 2;
idxtype[HDU_TABLE] = 3;
// icon size
int height = GetCharHeight();
int size = int(height + 1);
// icons
icons.push_back(DrawIcon(size,HDU_HEAD)); // unknown
icons.push_back(DrawIcon(size,HDU_HEAD));
icons.push_back(DrawIcon(size,HDU_IMAGE));
icons.push_back(DrawIcon(size,HDU_TABLE));
// Moon
phases = MoonPhases(height);
Init();
Bind(wxEVT_DATAVIEW_SELECTION_CHANGED,&MuniExtensionList::OnExtChanged,this);
}
void MuniExtensionList::Clear()
{
DeleteAllItems();
ClearColumns();
}
void MuniExtensionList::Init()
{
wxASSERT(GetItemCount() == 0 && GetColumnCount() == 0);
int chwidth = GetCharWidth();
timer.Start();
AppendIconTextColumn(LABEL,wxDATAVIEW_CELL_INERT,15*chwidth);
AppendIconTextColumn(L"⇩",wxDATAVIEW_CELL_INERT,2*chwidth);
}
void MuniExtensionList::Finish()
{
wxASSERT(GetItemCount() > 0 && GetColumnCount() == 2);
wxDataViewColumn *col = GetColumn(1);
DeleteColumn(col);
SelectRow(0);
}
void MuniExtensionList::Append(const wxString& extname, int hdutype)
{
wxASSERT(GetColumnCount() == 2);
wxLogDebug("MuniExtensionList::Append: "+extname+": %d %d",hdutype,int(GetItemCount()));
const size_t maxlen = 15;
wxString label(extname);
if( label.Len() > maxlen )
label = label.Truncate(maxlen) + "...";
wxVariant name;
name << wxDataViewIconText(label, icons[hdutype]);
wxVariant moonicon;
moonicon << wxDataViewIconText("", phases[0]);
wxVector<wxVariant> cols;
cols.push_back(name);
cols.push_back(moonicon);
AppendItem(cols);
timer.Start();
}
void MuniExtensionList::FinishProgress(int chdu)
{
UpdateProgress(chdu,1.01);
}
void MuniExtensionList::UpdateProgress(int chdu, double f)
{
int n = int(28*f);
if( GetItemCount() == chdu ) {
if( timer.Time() > 250 ) {
wxVariant moonicon;
moonicon << wxDataViewIconText("",phases[n]);
SetValue(moonicon,chdu-1,1);
}
}
/*
There is a wx/GTK trouble.
The warning:
(xmunipack:31084): Gtk-CRITICAL **: 12:24:47.105: gtk_widget_queue_draw_area: assertion 'width >= 0' failed
is issued shortly after start when something is probably uninitialised.
I found the only solution: Moon icon should be draw after some delay.
*/
}
void MuniExtensionList::ChangeSelection(int n)
{
SelectRow(n);
}
void MuniExtensionList::OnExtChanged(wxDataViewEvent& event)
{
if( ! IsOk() ) return;
int n = GetSelectedRow();
// wxLogDebug("MuniExtensionList::OnExtChanged %d",n);
if( n >= 0 ) {
wxCommandEvent e(wxEVT_COMMAND_MENU_SELECTED,ID_EXTENSION_SELECT);
e.SetInt(n);
wxQueueEvent(GetParent(),e.Clone());
}
}
bool MuniExtensionList::IsOk() const
{
return GetItemCount() > 0 && GetColumnCount() == 1;
}
wxIcon MuniExtensionList::DrawIcon(int size, int hdutype) const
{
wxBitmap bmp(size,size);
wxMemoryDC mdc(bmp);
wxGraphicsContext *gc = wxGraphicsContext::Create(mdc);
if( gc ) {
wxString ch;
if( hdutype == HDU_IMAGE )
ch = config->image_symbol;
else if( hdutype == HDU_TABLE )
ch = config->table_symbol;
else if( hdutype == HDU_HEAD )
ch = config->head_symbol;
gc->SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX)));
gc->DrawRectangle(0,0,size,size);
double w,h,u,v;
gc->SetFont(*wxNORMAL_FONT,*wxBLACK);
gc->GetTextExtent(ch,&w,&h,&u,&v);
int x = wxMax((size - w) / 2,0);
int y = wxMax((size - h) / 2,0);
gc->DrawText(ch,x,y);
delete gc;
}
mdc.SelectObject(wxNullBitmap);
wxIcon icon;
icon.CopyFromBitmap(bmp);
return icon;
}
vector<wxIcon> MuniExtensionList::MoonPhases(int size) const
{
const int width = 48;
const int height = 48;
wxImage moon_56frames(config->moon_56frames);
vector<wxIcon> icons;
for(int i = 0; i < 56; i++)
{
wxRect rect(1+i*width,1,width,height);
wxImage sub = moon_56frames.GetSubImage(rect);
wxBitmap bmp(sub.Rescale(size,size));
wxIcon icon;
icon.CopyFromBitmap(bmp);
icons.push_back(icon);
}
return icons;
}
|