reorganization of Groups dialog (unstable - needs to be checked)

This commit is contained in:
ars_goldendict 2009-04-26 13:16:30 +00:00
parent dae521a262
commit f5f23ff4b2
10 changed files with 851 additions and 454 deletions

View file

@ -1,5 +1,5 @@
<RCC>
<qresource>
<qresource prefix="/" >
<file>flags/wiki.png</file>
<file>flags/ad.png</file>
<file>flags/ae.png</file>
@ -248,5 +248,7 @@
<file>flags/za.png</file>
<file>flags/zm.png</file>
<file>flags/zw.png</file>
<file>flags/en.png</file>
<file>flags/uk.png</file>
</qresource>
</RCC>

BIN
src/flags/en.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 599 B

BIN
src/flags/uk.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 446 B

View file

@ -27,8 +27,18 @@ Groups::Groups( QWidget * parent,
this, SLOT( addNew() ) );
connect( ui.renameGroup, SIGNAL( clicked() ),
this, SLOT( renameCurrent() ) );
// connect( ui.emptyGroup, SIGNAL( clicked() ),
// this, SLOT( emptyCurrent() ) );
connect( ui.removeGroup, SIGNAL( clicked() ),
this, SLOT( removeCurrent() ) );
connect( ui.removeGroups, SIGNAL( clicked() ),
this, SLOT( removeAll() ) );
connect( ui.tbAddDictsToGroup, SIGNAL( clicked() ),
this, SLOT( addDictsToCurrentGroup() ) );
connect( ui.tbRemoveDictsFromGroup, SIGNAL( clicked() ),
this, SLOT( removeDictsFromCurrentGroup() ) );
countChanged();
}
@ -43,7 +53,12 @@ void Groups::countChanged()
bool en = ui.groups->count();
ui.renameGroup->setEnabled( en );
// ui.emptyGroup->setEnabled( en );
ui.removeGroup->setEnabled( en );
ui.removeGroups->setEnabled( en );
ui.tbAddDictsToGroup->setEnabled( en );
ui.tbRemoveDictsFromGroup->setEnabled( en );
}
void Groups::addNew()
@ -78,6 +93,38 @@ void Groups::renameCurrent()
ui.groups->renameCurrentGroup( name );
}
//void Groups::emptyCurrent()
//{
// int current = ui.groups->currentIndex();
//
// if ( current < 0 || QMessageBox::question( this, tr( "Empty group contents" ),
// tr( "Are you sure you want to remove all the dictionaries from this group?" ),
// QMessageBox::Yes, QMessageBox::Cancel ) != QMessageBox::Yes )
// return;
//
// ui.groups->emptyCurrentGroup();
//}
void Groups::addDictsToCurrentGroup()
{
int current = ui.groups->currentIndex();
if ( current >= 0 )
{
ui.groups->getCurrentModel()->addSelectedUniqueFromModel( ui.dictionaries->selectionModel() );
}
}
void Groups::removeDictsFromCurrentGroup()
{
int current = ui.groups->currentIndex();
if ( current >= 0 )
{
ui.groups->getCurrentModel()->removeSelectedRows( ui.groups->getCurrentSelectionModel() );
}
}
void Groups::removeCurrent()
{
int current = ui.groups->currentIndex();
@ -90,3 +137,16 @@ void Groups::removeCurrent()
countChanged();
}
}
void Groups::removeAll()
{
int current = ui.groups->currentIndex();
if ( current >= 0 && QMessageBox::question( this, tr( "Remove all groups" ),
tr( "Are you sure you want to remove all the groups?" ),
QMessageBox::Yes, QMessageBox::Cancel ) == QMessageBox::Yes )
{
ui.groups->removeAllGroups();
countChanged();
}
}

View file

@ -30,10 +30,13 @@ private:
void countChanged();
private slots:
void addNew();
void renameCurrent();
//void emptyCurrent();
void addDictsToCurrentGroup();
void removeDictsFromCurrentGroup();
void removeCurrent();
void removeAll();
};
#endif

View file

@ -1,3 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Groups</class>
<widget class="QDialog" name="Groups">
@ -8,47 +9,138 @@
<rect>
<x>0</x>
<y>0</y>
<width>726</width>
<height>535</height>
<width>784</width>
<height>505</height>
</rect>
</property>
<property name="windowTitle">
<string>Groups</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2" >
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2" >
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<layout class="QVBoxLayout" name="verticalLayout_3" >
<item>
<widget class="QLabel" name="label_2" >
<property name="text" >
<string>Dictionaries available:</string>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Dictionaries available</string>
</property>
</widget>
</item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="DictListWidget" name="dictionaries"/>
</item>
</layout>
</widget>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label_3" >
<widget class="QToolButton" name="tbAddDictsToGroup">
<property name="minimumSize">
<size>
<width>32</width>
<height>32</height>
</size>
</property>
<property name="text">
<string>Groups:</string>
<string>&gt;</string>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="tbRemoveDictsFromGroup">
<property name="minimumSize">
<size>
<width>32</width>
<height>32</height>
</size>
</property>
<property name="text">
<string>&lt;</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>User groups</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="addGroup">
<property name="toolTip">
<string>Create a new dictionary group</string>
</property>
<property name="text">
<string>&amp;Add group</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="renameGroup">
<property name="toolTip">
<string>Change current group's name</string>
</property>
<property name="text">
<string>Re&amp;name group</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="removeGroup">
<property name="toolTip">
<string>Remove group</string>
</property>
<property name="text">
<string>&amp;Remove group</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="removeGroups">
<property name="toolTip">
<string>Remove all groups</string>
</property>
<property name="text">
<string>Remove all groups</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="DictGroupsWidget" name="groups">
<property name="tabPosition">
<enum>QTabWidget::North</enum>
</property>
<property name="tabShape">
<enum>QTabWidget::Rounded</enum>
</property>
<property name="elideMode">
<enum>Qt::ElideRight</enum>
</property>
<property name="documentMode">
<bool>false</bool>
</property>
<widget class="QWidget" name="tab_4">
<attribute name="title">
<string>Tab 2</string>
@ -56,37 +148,24 @@
</widget>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout" >
<item>
<widget class="QPushButton" name="addGroup" >
<property name="text" >
<string>&amp;Add group</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="renameGroup" >
<property name="text" >
<string>Re&amp;name group</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="removeGroup" >
<property name="text" >
<string>&amp;Remove group</string>
</property>
</layout>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label_4">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="text">
<string>Drag &amp; drop dictionaries to and from the groups, move them inside the groups, reorder the groups using your mouse.</string>
</property>
@ -97,6 +176,12 @@
</item>
<item>
<widget class="QDialogButtonBox" name="buttons">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
@ -106,6 +191,8 @@
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>

View file

@ -79,9 +79,80 @@ std::vector< sptr< Dictionary::Class > > const &
return dictionaries;
}
void DictListModel::removeSelectedRows( QItemSelectionModel * source )
{
if ( !source )
return;
QModelIndexList rows = source->selectedRows();
if ( !rows.count() )
return;
for ( int i = rows.count()-1; i >= 0; --i )
{
dictionaries.erase( dictionaries.begin() + rows.at( i ).row() );
}
reset();
}
void DictListModel::addSelectedUniqueFromModel( QItemSelectionModel * source )
{
if ( !source )
return;
QModelIndexList rows = source->selectedRows();
if ( !rows.count() )
return;
const DictListModel * baseModel = dynamic_cast< const DictListModel * > ( source->model() );
if ( !baseModel )
return;
QVector< Dictionary::Class * > list;
QVector< Dictionary::Class * > dicts;
for ( int i = 0; i < dictionaries.size(); i++ )
dicts.append( dictionaries.at( i ).get() );
for ( int i = 0; i < rows.count(); i++ )
{
Dictionary::Class * d =
static_cast< Dictionary::Class * > ( rows.at( i ).internalPointer() );
//qDebug() << "rows.at( i ).internalPointer() " << rows.at( i ).internalPointer();
if ( !d )
continue;
// sptr< Dictionary::Class > s ( d );
//
// if ( std::find( dictionaries.begin(), dictionaries.end(), s ) == dictionaries.end() )
// continue;
//
// qDebug() << "std::find ";
if ( !dicts.contains( d ) )
list.append( d );
//list.push_back( s );
}
if ( list.empty() )
return;
// qDebug() << "list " << list.size();
for ( int i = 0; i < list.size(); i++ )
dictionaries.push_back( sptr< Dictionary::Class > ( list.at( i ) ) );
reset();
}
Qt::ItemFlags DictListModel::flags( QModelIndex const & index ) const
{
Qt::ItemFlags defaultFlags = QAbstractListModel::flags( index );
Qt::ItemFlags defaultFlags = QAbstractItemModel::flags( index );
if (index.isValid())
return Qt::ItemIsDragEnabled | defaultFlags;
@ -101,6 +172,10 @@ QVariant DictListModel::data( QModelIndex const & index, int role ) const
if ( !item )
return QVariant();
switch ( index.column() )
{
case 0:
{
switch ( role )
{
case Qt::DisplayRole :
@ -117,6 +192,51 @@ QVariant DictListModel::data( QModelIndex const & index, int role ) const
default:;
}
break;
}
case 1:
{
switch ( role )
{
case Qt::DisplayRole :
return LangCoder::decode( item->getLangFrom() ).left( 3 );
case Qt::EditRole :
break;
case Qt::DecorationRole:
return LangCoder::icon( item->getLangFrom() );
default:;
}
break;
}
case 2:
{
switch ( role )
{
case Qt::DisplayRole :
return LangCoder::decode( item->getLangTo() ).left( 3 );
case Qt::EditRole :
break;
case Qt::DecorationRole:
return LangCoder::icon( item->getLangTo() );
default:;
}
break;
}
default:;
}
return QVariant();
}
@ -141,7 +261,7 @@ bool DictListModel::removeRows( int row, int count,
beginRemoveRows( parent, row, row + count - 1 );
dictionaries.erase( dictionaries.begin() + row,
dictionaries.begin() + row + count );
count == INT_MAX ? dictionaries.end() : dictionaries.begin() + row + count );
endRemoveRows();
return true;
@ -160,45 +280,80 @@ bool DictListModel::setData( QModelIndex const & index, const QVariant & value,
return true;
}
if ( role == Qt::EditRole )
{
Config::Group g;
g.dictionaries.push_back( Config::DictionaryRef( value.toString(), QString() ) );
Instances::Group i( g, *allDicts );
if ( i.dictionaries.size() == 1 )
{
// Found that dictionary
dictionaries[ index.row() ] = i.dictionaries.front();
emit dataChanged( index, index );
return true;
}
}
// if ( role == Qt::EditRole )
// {
// Config::Group g;
//
// g.dictionaries.push_back( Config::DictionaryRef( value.toString(), QString() ) );
//
// Instances::Group i( g, *allDicts );
//
// if ( i.dictionaries.size() == 1 )
// {
// // Found that dictionary
// dictionaries[ index.row() ] = i.dictionaries.front();
//
// emit dataChanged( index, index );
//
// return true;
// }
// }
return false;
}
Qt::DropActions DictListModel::supportedDropActions() const
{
return Qt::MoveAction;
return 0; //Qt::MoveAction;
}
QModelIndex DictListModel::index( int row, int column, const QModelIndex &parent ) const
{
return createIndex( row, column, dictionaries.at( row ).get() );
}
QModelIndex DictListModel::parent( const QModelIndex & child ) const
{
return QModelIndex();
}
QVariant DictListModel::headerData ( int section, Qt::Orientation orientation, int role ) const
{
if ( role == Qt::DisplayRole )
{
switch ( section )
{
case 0: return tr( "Dictionary" );
case 1: return tr( "Source" );
case 2: return tr( "Target" );
default:;
}
}
return QAbstractItemModel::headerData( section, orientation, role );
}
/// DictListWidget
DictListWidget::DictListWidget( QWidget * parent ): QListView( parent ),
DictListWidget::DictListWidget( QWidget * parent ): QTreeView( parent ),
model( this )
{
setModel( &model );
setSelectionMode( ExtendedSelection );
setDragEnabled( true );
setAcceptDrops( true );
setDropIndicatorShown( true );
setDragEnabled( false );
setAcceptDrops( false );
setDropIndicatorShown( false );
setRootIsDecorated( false );
setItemsExpandable( false );
// setSortingEnabled( true );
header()->setAlternatingRowColors( true );
header()->setResizeMode( QHeaderView::ResizeToContents );
// header()->setSortIndicatorShown( true );
// header()->setSortIndicator( 1, Qt::AscendingOrder );
}
DictListWidget::~DictListWidget()
@ -281,6 +436,32 @@ Config::Groups DictGroupsWidget::makeGroups() const
return result;
}
DictListModel * DictGroupsWidget::getCurrentModel() const
{
int current = currentIndex();
if ( current >= 0 )
{
DictGroupWidget * w = ( DictGroupWidget * ) widget( current );
return w->getModel();
}
return 0;
}
QItemSelectionModel * DictGroupsWidget::getCurrentSelectionModel() const
{
int current = currentIndex();
if ( current >= 0 )
{
DictGroupWidget * w = ( DictGroupWidget * ) widget( current );
return w->getSelectionModel();
}
return 0;
}
void DictGroupsWidget::addNewGroup( QString const & name )
{
if ( !allDicts )
@ -317,6 +498,17 @@ void DictGroupsWidget::renameCurrentGroup( QString const & name )
setTabText( current, escapeAmps( name ) );
}
void DictGroupsWidget::emptyCurrentGroup()
{
int current = currentIndex();
if ( current >= 0 )
{
DictGroupWidget * w = ( DictGroupWidget * ) widget( current );
w->getModel()->removeRows( );
}
}
void DictGroupsWidget::removeCurrentGroup()
{
int current = currentIndex();
@ -328,3 +520,14 @@ void DictGroupsWidget::removeCurrentGroup()
delete w;
}
}
void DictGroupsWidget::removeAllGroups()
{
while ( count() )
{
QWidget * w = widget( 0 );
removeTab( 0 );
delete w;
}
}

View file

@ -6,20 +6,20 @@
// Various custom widgets used in the Groups dialog
#include <QListWidget>
#include <QtGui>
#include "config.hh"
#include "dictionary.hh"
#include <vector>
/// A model to be projected into the view, according to Qt's MVC model
class DictListModel: public QAbstractListModel
class DictListModel: public QAbstractItemModel
{
Q_OBJECT
public:
DictListModel( QWidget * parent ):
QAbstractListModel( parent ), isSource( false ), allDicts( 0 )
QAbstractItemModel( parent ), isSource( false ), allDicts( 0 )
{}
/// Populates the current model with the given dictionaries. This is
@ -32,12 +32,20 @@ public:
/// Returns the dictionaries the model currently has listed
std::vector< sptr< Dictionary::Class > > const & getCurrentDictionaries() const;
void removeSelectedRows( QItemSelectionModel * source );
void addSelectedUniqueFromModel( QItemSelectionModel * source );
Qt::ItemFlags flags( QModelIndex const &index ) const;
int rowCount( QModelIndex const & parent ) const;
int columnCount( QModelIndex const & parent ) const { return 3; }
QVariant data( QModelIndex const & index, int role ) const;
bool insertRows( int row, int count, const QModelIndex & parent );
bool removeRows( int row, int count, const QModelIndex & parent );
bool insertRows( int row, int count, const QModelIndex & parent = QModelIndex() );
bool removeRows( int row = 0, int count = INT_MAX, const QModelIndex & parent = QModelIndex() );
bool setData( QModelIndex const & index, const QVariant & value, int role );
QModelIndex index( int row, int column,
const QModelIndex &parent = QModelIndex() ) const;
QModelIndex parent( const QModelIndex &child ) const;
QVariant headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
Qt::DropActions supportedDropActions() const;
@ -50,7 +58,7 @@ private:
/// This widget is for dictionaries' lists, it handles drag-n-drop operations
/// with them etc.
class DictListWidget: public QListView
class DictListWidget: public QTreeView
{
Q_OBJECT
public:
@ -67,6 +75,8 @@ public:
/// Returns the dictionaries the widget currently has listed
std::vector< sptr< Dictionary::Class > > const & getCurrentDictionaries() const;
DictListModel * getModel()
{ return & model; }
private:
@ -90,6 +100,12 @@ public:
/// (it is known by the containing tab widget only), it is returned as empty.
Config::Group makeGroup() const;
DictListModel * getModel() const
{ return ui.dictionaries->getModel(); }
QItemSelectionModel * getSelectionModel() const
{ return ui.dictionaries->selectionModel(); }
private:
Ui::DictGroupWidget ui;
unsigned groupId;
@ -117,12 +133,21 @@ public:
/// Changes the name of the currently chosen group, if any, to the given one
void renameCurrentGroup( QString const & );
void emptyCurrentGroup();
/// Removes the currently chosen group, if any
void removeCurrentGroup();
/// Removes all the groups
void removeAllGroups();
/// Creates groups from what is currently set up
Config::Groups makeGroups() const;
DictListModel * getCurrentModel() const;
QItemSelectionModel * getCurrentSelectionModel() const;
private:
unsigned nextId;

View file

@ -14,21 +14,36 @@ LangCoder::LangCoder()
const LangCode &lc = LangCodes[i];
if (lc.lang[0] == 0)
break;
//ls.order = i;
//ls.icon = QIcon(":/flags/" + QString(lc.code) + ".png");
codeMap[code2toInt(lc.code)] = i;
}
}
QString LangCoder::decode(quint32 code)
{
// temp!
if (langCoder.codeMap.contains(code))
return LangCodes[langCoder.codeMap[code]].lang;
return QString();
}
QIcon LangCoder::icon(quint32 code)
{
if (langCoder.codeMap.contains(code))
{
const LangCode &lc = LangCodes[ langCoder.codeMap[ code ] ];
// QString flag_id( lc.code );
// if (flag_id == "en")
// flag_id = "gb";
// else
// if (flag_id == "uk")
// flag_id = "ua";
return QIcon( ":/flags/" + QString( lc.code) + ".png" );
}
return QIcon();
}
LangStruct LangCoder::langStruct(quint32 code)
{
LangStruct ls;

View file

@ -237,6 +237,8 @@ public:
/// Returns decoded name of language or empty string if not found.
static QString decode(quint32 code);
/// Returns icon for language or empty string if not found.
static QIcon icon(quint32 code);
//const QMap<quint32, int>& codes() { return codeMap; }
@ -246,12 +248,12 @@ public:
// bool CheckCode(QString &code);
protected:
private:
QMap<quint32, int> codeMap;
// LangStruct dummyLS;
};
extern LangCoder langCoder;
//extern LangCoder langCoder;
///////////////////////////////////////////////////////////////////////////////