File size: 3,586 Bytes
a5ffdcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/****************************************************************************
**
** This file is part of the LibreCAD project, a 2D CAD program
**
** Copyright (C) 2011 Rallaz (rallazz@gmail.com)
** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
**
**
** This file is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
** 
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
**
** This copyright notice MUST APPEAR in all copies of the script!  
**
**********************************************************************/
#include "qc_actiongetselect.h"

#include <QKeyEvent>

#include "doc_plugin_interface.h"
#include "rs_actionselectsingle.h"
#include "rs_graphicview.h"

class Plugin_Entity;

QC_ActionGetSelect::QC_ActionGetSelect(LC_ActionContext* actionContext)
    :RS_ActionInterface("Get Select", actionContext, RS2::ActionGetSelect)
    , m_completed(false)
    , m_message(std::make_unique<QString>(tr("Select objects:"))){
}

QC_ActionGetSelect::QC_ActionGetSelect(RS2::EntityType typeToSelect, LC_ActionContext* actionContext)
    :RS_ActionInterface("Get Select", actionContext, RS2::ActionGetSelect)
    , m_completed(false)
    , m_message(std::make_unique<QString>(tr("Select objects:"))),
     m_entityTypeToSelect(typeToSelect){
}

QC_ActionGetSelect::~QC_ActionGetSelect() = default;

void QC_ActionGetSelect::updateMouseButtonHints() {
    switch (getStatus()) {
        case Select:
            updateMouseWidget(*m_message, tr("Cancel"));
            break;
        default:
            updateMouseWidget();
            break;
    }
}

RS2::CursorType QC_ActionGetSelect::doGetMouseCursor([[maybe_unused]] int status){
    return RS2::SelectCursor;
}

void QC_ActionGetSelect::setMessage(QString msg){
    *m_message = std::move(msg);
}

void QC_ActionGetSelect::init(int status) {
        RS_ActionInterface::init(status);
        m_graphicView->setCurrentAction(
                std::make_shared<RS_ActionSelectSingle>(m_entityTypeToSelect,  m_actionContext, this));
}

void QC_ActionGetSelect::mouseReleaseEvent(QMouseEvent* e) {
    if (e->button()==Qt::RightButton) {
        m_completed = true;
        updateMouseWidget();
        finish();
    }
}

void QC_ActionGetSelect::keyPressEvent(QKeyEvent* e){
    if (e->key()==Qt::Key_Escape || e->key()==Qt::Key_Enter){
        updateMouseWidget();
        finish();
        m_completed = true;
    }
}

/**
 * Adds all selected entities from 'container' to the selection.
 */
void QC_ActionGetSelect::getSelected(QList<Plug_Entity *> *se, Doc_plugin_interface *d) const{
    for (auto e: *m_container) {
        if (e->isSelected()) {
            Plugin_Entity *pe = new Plugin_Entity(e, d);
            se->append(reinterpret_cast<Plug_Entity *>(pe));
        }
    }
}

void QC_ActionGetSelect::unselectEntities(){
    for(auto e: *m_container){ // fixme - iterating all entities for selection
        if (e->isSelected()) {
            e->setSelected(false);
        }
    }
    updateSelectionWidget();
}