goldendict-ng/src/ui/groups_widgets.hh

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

267 lines
6.7 KiB
C++
Raw Normal View History

2012-02-20 21:47:14 +00:00
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#ifndef __GROUPS_WIDGETS_HH_INCLUDED__
#define __GROUPS_WIDGETS_HH_INCLUDED__
// Various custom widgets used in the Groups dialog
#include <vector>
#include <QAction>
#include <QListWidget>
#include <QLineEdit>
#include <QSortFilterProxyModel>
#include "config.hh"
#include "dict/dictionary.hh"
/// A model to be projected into the view, according to Qt's MVC model
class DictListModel: public QAbstractListModel
{
Q_OBJECT
public:
DictListModel( QWidget * parent ):
QAbstractListModel( parent ),
isSource( false ),
allDicts( 0 )
{
}
/// Populates the current model with the given dictionaries. This is
/// ought to be part of construction process.
void populate( std::vector< sptr< Dictionary::Class > > const & active,
std::vector< sptr< Dictionary::Class > > const & available );
2012-12-18 12:33:53 +00:00
void populate( std::vector< sptr< Dictionary::Class > > const & active );
/// Marks that this model is used as an immutable dictionary source
void setAsSource();
2023-06-23 01:25:15 +00:00
bool sourceModel() const
{
return isSource;
}
/// Returns the dictionaries the model currently has listed
std::vector< sptr< Dictionary::Class > > const & getCurrentDictionaries() const;
void removeSelectedRows( QItemSelectionModel * source );
void addSelectedUniqueFromModel( QItemSelectionModel * source );
2023-06-23 01:25:15 +00:00
Qt::ItemFlags flags( QModelIndex const & index ) const override;
int rowCount( QModelIndex const & parent ) const override;
QVariant data( QModelIndex const & index, int role ) const override;
bool insertRows( int row, int count, const QModelIndex & parent ) override;
bool removeRows( int row, int count, const QModelIndex & parent ) override;
bool setData( QModelIndex const & index, const QVariant & value, int role ) override;
2023-06-23 01:25:15 +00:00
void addRow( const QModelIndex & parent, sptr< Dictionary::Class > dict );
2023-06-23 01:25:15 +00:00
Qt::DropActions supportedDropActions() const override;
2009-05-03 19:24:25 +00:00
void filterDuplicates();
private:
bool isSource;
std::vector< sptr< Dictionary::Class > > dictionaries;
std::vector< sptr< Dictionary::Class > > const * allDicts;
2012-12-21 20:20:54 +00:00
signals:
void contentChanged();
};
/// This widget is for dictionaries' lists, it handles drag-n-drop operations
/// with them etc.
class DictListWidget: public QListView
{
Q_OBJECT
public:
DictListWidget( QWidget * parent );
2023-06-23 01:25:15 +00:00
~DictListWidget() override = default;
/// Populates the current list with the given dictionaries.
void populate( std::vector< sptr< Dictionary::Class > > const & active,
std::vector< sptr< Dictionary::Class > > const & available );
2012-12-18 12:33:53 +00:00
void populate( std::vector< sptr< Dictionary::Class > > const & active );
/// Marks that this widget is used as an immutable dictionary source
void setAsSource();
/// Returns the dictionaries the widget currently has listed
std::vector< sptr< Dictionary::Class > > const & getCurrentDictionaries() const;
DictListModel * getModel()
2023-06-23 01:25:15 +00:00
{
return &model;
}
signals:
void gotFocus();
protected:
2023-06-23 01:25:15 +00:00
void dropEvent( QDropEvent * event ) override;
void focusInEvent( QFocusEvent * ) override;
2023-06-23 01:25:15 +00:00
void rowsAboutToBeRemoved( QModelIndex const & parent, int start, int end ) override;
private:
DictListModel model;
};
#include "ui_dictgroupwidget.h"
/// A widget that is placed into each tab in the Groups dialog.
class DictGroupWidget: public QWidget
{
Q_OBJECT
public:
DictGroupWidget( QWidget * parent, std::vector< sptr< Dictionary::Class > > const &, Config::Group const & );
Config::Group makeGroup() const;
DictListModel * getModel() const
{
return ui.dictionaries->getModel();
}
QItemSelectionModel * getSelectionModel() const
{
return ui.dictionaries->selectionModel();
}
QString name()
{
return groupName;
}
void setName( const QString & name )
{
groupName = name;
}
private slots:
void groupIconActivated( int );
void showDictInfo( const QPoint & pos );
void removeCurrentItem( QModelIndex const & );
private:
Ui::DictGroupWidget ui;
unsigned groupId;
QString groupName;
signals:
void showDictionaryInfo( QString const & id );
};
/// A tab widget with groups inside
class DictGroupsWidget: public QTabWidget
{
Q_OBJECT
public:
DictGroupsWidget( QWidget * parent );
/// Creates all the tabs with the groups
void populate( Config::Groups const &,
2012-12-18 12:35:11 +00:00
std::vector< sptr< Dictionary::Class > > const & allDicts,
std::vector< sptr< Dictionary::Class > > const & activeDicts );
/// Creates new empty group with the given name
int addNewGroup( QString const & );
2012-12-21 11:48:41 +00:00
/// Creates new empty group with the given name if no such group
/// and return it index
int addUniqueGroup( QString const & name );
void addAutoGroups();
/// auto grouping by containning folder
void addAutoGroupsByFolders();
2023-04-29 06:59:19 +00:00
void addGroupBasedOnMap( const QMultiMap< QString, sptr< Dictionary::Class > > & groupToDicts );
void groupsByMetadata();
/// Returns currently chosen group's name
QString getCurrentGroupName() const;
/// Changes the name of the currently chosen group, if any, to the given one
void renameCurrentGroup( QString const & );
/// 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;
DictListModel * getModelAt( int current ) const;
2023-06-22 08:30:41 +00:00
int getDictionaryCountAt( int current ) const;
std::vector< sptr< Dictionary::Class > > getDictionaryAt( int current ) const;
QItemSelectionModel * getCurrentSelectionModel() const;
private:
2012-12-21 16:46:17 +00:00
/// Add source group to target group
void combineGroups( int source, int target );
unsigned nextId;
std::vector< sptr< Dictionary::Class > > const * allDicts;
2012-12-18 12:35:11 +00:00
std::vector< sptr< Dictionary::Class > > const * activeDicts;
2012-12-21 11:48:41 +00:00
private slots:
void contextMenu( QPoint const & );
2012-12-21 20:20:54 +00:00
void tabDataChanged();
2012-12-21 11:48:41 +00:00
signals:
void showDictionaryInfo( QString const & id );
};
class QuickFilterLine: public QLineEdit
{
Q_OBJECT
public:
QuickFilterLine( QWidget * parent );
2023-06-23 01:25:15 +00:00
~QuickFilterLine() override;
/// Sets the source view to filter
void applyTo( QAbstractItemView * source );
QAction * getFocusAction()
{
return &m_focusAction;
}
QModelIndex mapToSource( QModelIndex const & idx );
protected:
2023-06-23 01:25:15 +00:00
void keyPressEvent( QKeyEvent * event ) override;
private:
QSortFilterProxyModel m_proxyModel;
QAction m_focusAction;
QAbstractItemView * m_source;
private slots:
void filterChangedInternal();
void emitFilterChanged();
void focusFilterLine();
signals:
void filterChanged( QString const & filter );
};
#endif