Allow using any file as an icon for the dictionary group.

This commit is contained in:
Konstantin Isakov 2010-07-05 22:36:03 +04:00
parent d9c23a07e5
commit 0140321b78
6 changed files with 99 additions and 7 deletions

View file

@ -183,6 +183,9 @@ Group loadGroup( QDomElement grp, unsigned * nextId = 0 )
g.name = grp.attribute( "name" );
g.icon = grp.attribute( "icon" );
if ( !grp.attribute( "iconData" ).isEmpty() )
g.iconData = QByteArray::fromBase64( grp.attribute( "iconData" ).toAscii() );
if ( !grp.attribute( "shortcut" ).isEmpty() )
g.shortcut = QKeySequence::fromString( grp.attribute( "shortcut" ) );
@ -637,6 +640,15 @@ void saveGroup( Group const & data, QDomElement & group )
group.setAttributeNode( icon );
}
if ( data.iconData.size() )
{
QDomAttr iconData = dd.createAttribute( "iconData" );
iconData.setValue( QString::fromAscii( data.iconData.toBase64() ) );
group.setAttributeNode( iconData );
}
if ( !data.shortcut.isEmpty() )
{
QDomAttr shortcut = dd.createAttribute( "shortcut" );

View file

@ -74,6 +74,7 @@ struct Group
{
unsigned id;
QString name, icon;
QByteArray iconData;
QKeySequence shortcut;
vector< DictionaryRef > dictionaries;
@ -81,7 +82,8 @@ struct Group
bool operator == ( Group const & other ) const
{ return id == other.id && name == other.name && icon == other.icon &&
dictionaries == other.dictionaries && shortcut == other.shortcut; }
dictionaries == other.dictionaries && shortcut == other.shortcut &&
iconData == iconData; }
bool operator != ( Group const & other ) const
{ return ! operator == ( other ); }

View file

@ -29,7 +29,14 @@ DictGroupWidget::DictGroupWidget( QWidget * parent,
QStringList icons = QDir( ":/flags/" ).entryList( QDir::Files, QDir::NoSort );
ui.groupIcon->addItem( "None", "" );
ui.groupIcon->addItem( tr( "None" ), "" );
bool usesIconData = !group.iconData.isEmpty();
if ( !usesIconData )
ui.groupIcon->addItem( tr( "From file..." ), "" );
else
ui.groupIcon->addItem( Instances::iconFromData( group.iconData ), group.icon );
for( int x = 0; x < icons.size(); ++x )
{
@ -39,11 +46,45 @@ DictGroupWidget::DictGroupWidget( QWidget * parent,
ui.groupIcon->addItem( QIcon( ":/flags/" + icons[ x ] ), n, icons[ x ] );
if ( icons[ x ] == group.icon )
ui.groupIcon->setCurrentIndex( x + 1 );
if ( !usesIconData && icons[ x ] == group.icon )
ui.groupIcon->setCurrentIndex( x + 2 );
}
if ( usesIconData )
ui.groupIcon->setCurrentIndex( 1 );
ui.shortcut->setHotKey( Config::HotKey( group.shortcut ) );
connect( ui.groupIcon, SIGNAL(activated(int)),this,SLOT(groupIconActivated(int)),
Qt::QueuedConnection );
}
void DictGroupWidget::groupIconActivated( int index )
{
if ( index == 1 )
{
QString chosenFile =
QFileDialog::getOpenFileName( this, tr( "Choose a file to use as group icon" ),
tr( "Images" ) +
" (*.png *.xpm *.jpg *.jpeg *.bmp *.gif);;;" +
tr( "All files" ) + " (*)" );
if ( !chosenFile.isEmpty() )
{
QIcon icon( chosenFile );
if ( icon.isNull() )
QMessageBox::critical( this, tr( "Error" ), tr( "Can't read the specified image file." ) );
else
{
ui.groupIcon->setItemIcon( 1, icon );
QString baseName = QFileInfo( chosenFile ).completeBaseName();
ui.groupIcon->setItemText( 1, baseName );
ui.groupIcon->setItemData( 1, baseName );
}
}
}
}
Config::Group DictGroupWidget::makeGroup() const
@ -54,7 +95,12 @@ Config::Group DictGroupWidget::makeGroup() const
g.dictionaries = ui.dictionaries->getCurrentDictionaries();
g.icon = ui.groupIcon->itemData( ui.groupIcon->currentIndex() ).toString();
int currentIndex = ui.groupIcon->currentIndex();
if ( currentIndex == 1 ) // File
g.iconData = ui.groupIcon->itemIcon( currentIndex );
g.icon = ui.groupIcon->itemData( currentIndex ).toString();
g.shortcut = ui.shortcut->getHotKey().toKeySequence();

View file

@ -111,6 +111,10 @@ public:
QItemSelectionModel * getSelectionModel() const
{ return ui.dictionaries->selectionModel(); }
private slots:
void groupIconActivated( int );
private:
Ui::DictGroupWidget ui;
unsigned groupId;

View file

@ -3,6 +3,7 @@
#include "instances.hh"
#include <set>
#include <QBuffer>
namespace Instances {
@ -16,6 +17,9 @@ Group::Group( Config::Group const & cfgGroup,
icon( cfgGroup.icon ),
shortcut( cfgGroup.shortcut )
{
if ( !cfgGroup.iconData.isEmpty() )
iconData = iconFromData( cfgGroup.iconData );
for( unsigned x = 0; x < cfgGroup.dictionaries.size(); ++x )
{
std::string id = cfgGroup.dictionaries[ x ].id.toStdString();
@ -59,6 +63,13 @@ Config::Group Group::makeConfigGroup()
result.icon = icon;
result.shortcut = shortcut;
if ( !iconData.isNull() )
{
QDataStream stream( &result.iconData, QIODevice::WriteOnly );
stream << iconData;
}
for( unsigned x = 0; x < dictionaries.size(); ++x )
result.dictionaries.push_back(
Config::DictionaryRef( dictionaries[ x ]->getId().c_str(),
@ -69,13 +80,15 @@ Config::Group Group::makeConfigGroup()
QIcon Group::makeIcon() const
{
if ( !iconData.isNull() )
return iconData;
QIcon i = icon.size() ?
QIcon( ":/flags/" + icon ) : QIcon();
return i;
}
Group * Groups::findGroup( unsigned id )
{
for( unsigned x = 0; x < size(); ++x )
@ -145,4 +158,15 @@ void updateNames( Config::Class & cfg,
updateNames( cfg.groups, allDictionaries );
}
QIcon iconFromData( QByteArray const & iconData )
{
QDataStream stream( iconData );
QIcon result;
stream >> result;
return result;
}
}

View file

@ -22,6 +22,7 @@ struct Group
{
unsigned id;
QString name, icon;
QIcon iconData;
QKeySequence shortcut;
vector< sptr< Dictionary::Class > > dictionaries;
@ -36,7 +37,7 @@ struct Group
/// Makes the configuration group from the current contents.
Config::Group makeConfigGroup();
/// Makes an icon object for this group, based on the icon's name.
/// Makes an icon object for this group, based on the icon's name or iconData.
QIcon makeIcon() const;
// Some constants
@ -82,6 +83,9 @@ void updateNames( Config::Groups &,
void updateNames( Config::Class &,
vector< sptr< Dictionary::Class > > const & allDictionaries );
/// Creates icon from icon data. Used by Group, but also by others who work
/// with icon data directly.
QIcon iconFromData( QByteArray const & );
}