AlgorithmSelector.cpp 9.99 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/*
 *	(c) LSIIT, UMR CNRS/UdS
 *	Authors: O. Gnevaux, F. Larue.
 *
 *	See licence.txt for additional information.
 */


#include "GLViewer.h"
#include "AlgorithmSelector.h"
#include "ui_AlgorithmSelector.h"
12
#include "UIMultiProgressDialog.h"
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
#include <QLabel>




AlgorithmSelector::AlgorithmSelector(QWidget *parent) :
    QWidget( parent ),
    m_ui( new Ui::AlgorithmSelector ),
    m_SelectedTool( NULL ),
    m_SelectedToolParam( NULL )
{
    m_ui->setupUi(this);
    m_ui->groupToolDescription->hide();

    connect( m_ui->buttonApplyTool, SIGNAL(clicked()), this, SLOT(emitApplySignal()) );

    m_CategoryButtonGroup = new QButtonGroup( this );
    connect( m_CategoryButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(selectCategory(int)) );

    m_ToolsByCategory.resize( ToolCategory::categoriesCount() );
    m_LastUsedInCategory.resize( ToolCategory::categoriesCount() );
    for( int i=0; i<ToolCategory::categoriesCount(); ++i )
        m_LastUsedInCategory[i] = 0;
}


AlgorithmSelector::~AlgorithmSelector()
{
    delete m_ui;
}


void AlgorithmSelector::changeEvent(QEvent *e)
{
    QWidget::changeEvent( e );
    switch( e->type() )
    {
        case QEvent::LanguageChange: m_ui->retranslateUi(this); break;
        default: break;
    }
}


void AlgorithmSelector::populateTools( ToolInterface *plugin )
{
    QList<int> toolIds;
    plugin->toolIds( toolIds );

    foreach( int i, toolIds )
    {
63
64
        int categoryId = plugin->category(i);
        if( ToolCategory::isCategoryDeclared(categoryId) )
65
66
67
68
69
        {
            ToolDescriptor tool;
            tool.plugin = plugin;
            tool.id     = i;
            tool.param  = new UIParamSet( "Tool options", this );
70
71
            plugin->setParameters( tool.param );
            plugin->declareParameters( i, *tool.param );
72
            tool.param->hide();
73
			//tool.param->setMaximumWidth(360);
74

75
76
            connect( tool.param, SIGNAL(parameterUpdated(UIParam*)), tool.plugin, SLOT(updateParameter(UIParam*)) );

77
            foreach( UIParamAction* act, tool.param->GetActions() )
78
                connect( act->Widget(), SIGNAL(clicked(bool)), this, SLOT(emitActionSignal()) );
79

80
            m_ToolsByName[ plugin->name(i) ] = tool;
81
82
83
84
85
86
87
            m_ToolsByCategory[categoryId].push_back( tool );
            m_ui->widgetToolGroupBoxes->layout()->addWidget( tool.param );
        }
    }
}


88
89
90
91
92
93
94
95
void AlgorithmSelector::releaseAllTools()
{
    for( auto &category : m_ToolsByCategory )
        for( auto &tool : category )
            delete tool.param;
}


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
void AlgorithmSelector::finalizeTools()
{
    // Sort tools by name for each category.
    for( QVector<QVector<ToolDescriptor>>::iterator category=m_ToolsByCategory.begin(); category!=m_ToolsByCategory.end(); ++category )
    {
        QMap<QString,ToolDescriptor> toolByName;
        foreach( ToolDescriptor tool, *category )
            toolByName[tool.plugin->name(tool.id)] = tool;

        category->clear();

        for( QMap<QString,ToolDescriptor>::iterator tool=toolByName.begin(); tool!=toolByName.end(); ++tool )
            category->push_back( *tool );
    }


    /** For each non-empty category, a pannel similar to the following one is created and added to the category widget.
     *      +--------------+
     *      | +----------+ |
     *      | |  Button  | |
     *      | |   with   | |
     *      | |   icon   | |
     *      | +----------+ |
     *      |     Label    |
     *      +--------------+
     */

123
124
125
126
    QPushButton *firstCategoryButton = NULL;
    int firstCategoryId = -1;

    for( int categoryId : ToolCategory::categoriesId() )
127
128
        if( !m_ToolsByCategory[categoryId].empty() )
        {
129
            QVBoxLayout *l = new QVBoxLayout();
130
131
132
133
134
135
136
137
138
            l->setAlignment( Qt::AlignHCenter );
            l->setContentsMargins( 0, 0, 0, 0 );
            l->setSpacing( 0 );

            QPushButton *categoryButton = new QPushButton( ToolCategory::categoryIcon(categoryId), QString() );
            categoryButton->setContentsMargins( 0, 0, 0, 0 );
            categoryButton->setIconSize( QSize(80,80) );
            categoryButton->setFlat( true );
            categoryButton->setMinimumSize( 80, 80 );
139
            categoryButton->setMaximumSize( 80, 80 );
140
            categoryButton->setCheckable( true );
141
            l->addWidget( categoryButton, 0, Qt::AlignHCenter );
142
143
            m_CategoryButtonGroup->addButton( categoryButton, categoryId );

144
145
146
147
148
149
            if( !firstCategoryButton )
            {
                firstCategoryButton = categoryButton;
                firstCategoryId = categoryId;
            }

150
            QLabel *categoryLabel = new QLabel( ToolCategory::categoryName(categoryId) );
151
            QFont f = QApplication::font();
152
153
154
            f.setPointSize( 7 );
            categoryLabel->setFont( f );
            categoryLabel->setAlignment( Qt::AlignHCenter );
155
            //categoryLabel->setWordWrap( true );
156
            l->addWidget( categoryLabel, 0, Qt::AlignHCenter );
157
158
159
160
161
162
163
164
165

            QWidget *categoryPannel = new QWidget();
            categoryPannel->setLayout( l );

            QGridLayout *allCategories = (QGridLayout*) m_ui->widgetCategories->layout();
            int i = allCategories->count();
            allCategories->addWidget( categoryPannel, i>>2, i&3 );
        }

166
167
168
169
170
171
172
	m_ui->widgetCategories->layout()->setAlignment(Qt::AlignHCenter | Qt::AlignTop);

    if( firstCategoryButton )
    {
        firstCategoryButton->setChecked( true );
        selectCategory( firstCategoryId );
    }
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
}


void AlgorithmSelector::selectCategory( int categoryId )
{
    disconnect( m_ui->comboAvailableTools, SIGNAL(currentIndexChanged(int)), this, SLOT(selectTool(int)) );

    m_SelectedCategory = categoryId;
    int lastUsedId = m_LastUsedInCategory[categoryId];

    // Populate the combobox with the names of all tools associated to the selected category.
    m_ui->comboAvailableTools->clear();
    foreach( ToolDescriptor tool, m_ToolsByCategory[categoryId] )
        m_ui->comboAvailableTools->addItem( tool.plugin->name(tool.id) );

    m_ui->comboAvailableTools->setCurrentIndex( lastUsedId );
    selectTool( lastUsedId );

    connect( m_ui->comboAvailableTools, SIGNAL(currentIndexChanged(int)), this, SLOT(selectTool(int)) );
}


void AlgorithmSelector::selectTool( int n )
{
    if( m_SelectedToolParam )
    {
        m_SelectedToolParam->hide();
        m_SelectedToolParam = NULL;
    }

    if( n >= 0 )
    {
        m_SelectedTool = &m_ToolsByCategory[m_SelectedCategory][n];

        QString description = m_SelectedTool->plugin->description( m_SelectedTool->id );
        if( description.isNull() || description.isEmpty() )
            m_ui->groupToolDescription->hide();
        else
        {
            m_ui->groupToolDescription->show();
            m_ui->labelToolDescription->setText( description );
            m_ui->labelToolDescription->setEnabled( true );

            QVector<QString> acceptedTypes;
            m_SelectedTool->plugin->acceptedDataTypes( m_SelectedTool->id, acceptedTypes );
            if( acceptedTypes.empty() )
                m_ui->labelToolDescription->setToolTip( "" );
            else
            {
                QString toolTip = "Accepted items:";
                foreach( QString t, acceptedTypes )
                    toolTip += QString("\n") + t;
                m_ui->labelToolDescription->setToolTip( toolTip );
            }
        }

229
        if( !m_SelectedTool->param->IsEmpty() )
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
        {
            m_SelectedToolParam = m_SelectedTool->param;
            m_SelectedToolParam->show();
        }

        m_LastUsedInCategory[m_SelectedCategory] = n;
    }
    else
    {
        m_ui->groupToolDescription->hide();
        m_SelectedTool = NULL;
    }
}


void AlgorithmSelector::emitApplySignal()
{
	if( m_SelectedTool )
	{
		m_ActionId = QString();
		emit askItemsToApplyToolOn();
	}
}


void AlgorithmSelector::emitActionSignal()
{
    m_ActionId = QString();
258
    foreach( UIParamAction* act, m_SelectedTool->param->GetActions() )
259
260
        if( act->Widget() == (QPushButton*)sender() )
            m_ActionId = act->Id();
261
262
263
264
265
266
267

    if( !m_ActionId.isNull() )
        emit askItemsToApplyToolOn();
}


void AlgorithmSelector::applyCurrentToolOn( const QList<GenericUIData*> &items,
268
                                            UIMainWindow *mainwin,
269
                                            GLViewer *glviewer,
270
                                            UIProject *project )
271
272
273
{
    if( m_SelectedTool )
    {
274
        UIContext ctx;
275
276
277
278
279
280
        ctx.mainwin        = mainwin;
        ctx.glviewer       = glviewer;
        ctx.project        = project;

        QVector<QString> acceptedTypes;
        m_SelectedTool->plugin->acceptedDataTypes( m_SelectedTool->id, acceptedTypes );
281

282
283
        UIToolDataSet dataSet;
        dataSet.items.resize( acceptedTypes.size() );
284
285
286
287

        foreach( GenericUIData *m, items )
            for( int k=0; k<acceptedTypes.size(); ++k )
                if( m->GetTypeString() == acceptedTypes[k] )
288
                    dataSet.items[k].push_back( m );
289

290
291
292
293
294
        unsigned int nProgressBars = m_SelectedTool->plugin->needProgressBars( m_SelectedTool->id );
        UIMultiProgressDialog *progressDlg = NULL;
        if( nProgressBars > 0 )
        {
            progressDlg = new UIMultiProgressDialog( nProgressBars, mainwin );
295
            progressDlg->setWindowTitle( m_SelectedTool->plugin->name(m_SelectedTool->id) );
296
297
298
299
            connect( m_SelectedTool->plugin, SIGNAL(notifyProgressBar(unsigned int,QString,int)), progressDlg, SLOT(setBar(unsigned int,QString,int)) );
            connect( m_SelectedTool->plugin, SIGNAL(notifyProgressValue(unsigned int,int)), progressDlg, SLOT(setProgress(unsigned int,int)) );
        }

300
301
        bool executionOk;
        if( m_ActionId.isNull() )
302
            executionOk = m_SelectedTool->plugin->exec( m_SelectedTool->id, dataSet, *m_SelectedToolParam, ctx );
303
        else
304
            executionOk = m_SelectedTool->plugin->execAction( m_SelectedTool->id, m_ActionId, dataSet, *m_SelectedToolParam, ctx );
305
306
307
308
309
310
311

        if( progressDlg )
        {
            progressDlg->close();
            progressDlg->deleteLater();
        }

312
313
314
        emit toolExecutionFinished( executionOk );
    }
}