1 /**
2  * UGENE - Integrated Bioinformatics Tools.
3  * Copyright (C) 2008-2017 UniPro <ugene@unipro.ru>
4  * http://ugene.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  */
21 #include "primitives/GTAction.h"
22 
23 #include <QAbstractButton>
24 #include <QApplication>
25 #include <QWidget>
26 
27 #include "primitives/GTWidget.h"
28 
29 namespace HI {
30 
31 #define GT_CLASS_NAME "GTAction"
32 
33 #define GT_METHOD_NAME "button"
button(GUITestOpStatus & os,const QString & objectName,QObject * parent,const GTGlobals::FindOptions & options)34 QAbstractButton *GTAction::button(GUITestOpStatus &os, const QString &objectName, QObject *parent, const GTGlobals::FindOptions &options) {
35     QAction *action = findAction(os, objectName, parent, options);
36     if (action == nullptr) {
37         return nullptr;
38     }
39     QAbstractButton *resultButton = button(os, action);
40     GT_CHECK_RESULT(resultButton != nullptr || !options.failIfNotFound, "Button with object name not found: " + objectName, nullptr);
41     return resultButton;
42 }
43 #undef GT_METHOD_NAME
44 
45 #define GT_METHOD_NAME "button"
button(GUITestOpStatus & os,const QAction * action)46 QAbstractButton *GTAction::button(GUITestOpStatus &os, const QAction *action) {
47     GT_CHECK_RESULT(action != nullptr, "action is NULL", nullptr);
48     QList<QWidget *> associatedWidgets = action->associatedWidgets();
49     for (QWidget *associatedWidget : associatedWidgets) {
50         if (auto button = qobject_cast<QAbstractButton *>(associatedWidget)) {
51             return button;
52         }
53     }
54     return nullptr;
55 }
56 #undef GT_METHOD_NAME
57 
58 #define GT_METHOD_NAME "findAction"
findAction(GUITestOpStatus & os,const QString & objectName,QObject * parent,const GTGlobals::FindOptions & options)59 QAction *GTAction::findAction(GUITestOpStatus &os, const QString &objectName, QObject *parent, const GTGlobals::FindOptions &options) {
60     QList<QAction *> actions = GTWidget::findChildren<QAction>(os, parent, [&objectName](auto action) { return action->objectName() == objectName; });
61     GT_CHECK_RESULT(actions.size() < 2, QString("There are %1 actions with object name %2").arg(actions.size()).arg(objectName), nullptr);
62     if (actions.size() == 1) {
63         return actions[0];
64     }
65     GT_CHECK_RESULT(!options.failIfNotFound, "Action with object name not found: " + objectName, nullptr);
66     return nullptr;
67 }
68 #undef GT_METHOD_NAME
69 
70 #define GT_METHOD_NAME "findActionByText"
findActionByText(GUITestOpStatus & os,const QString & text,QWidget * parent)71 QAction *GTAction::findActionByText(GUITestOpStatus &os, const QString &text, QWidget *parent) {
72     QList<QAction *> actions = GTWidget::findChildren<QAction>(os, parent, [text](auto action) { return action->text() == text; });
73     GT_CHECK_RESULT(!actions.isEmpty(), "Action with text not found: " + text, nullptr);
74     GT_CHECK_RESULT(actions.size() == 1, QString("There are %1 actions with text: %2").arg(actions.size()).arg(text), nullptr);
75     return actions[0];
76 }
77 #undef GT_METHOD_NAME
78 
79 #undef GT_CLASS_NAME
80 
81 }  // namespace HI
82