]> Shamusworld >> Repos - architektonas/blob - src/forms/commandwidget.cpp
GPL compliance check...
[architektonas] / src / forms / commandwidget.cpp
1 // commandwidget.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
9 //
10 // JLH = James L. Hammons <jlhamm@acm.org>
11 //
12 // Who  When        What
13 // ---  ----------  -----------------------------------------------------------
14 // JLH  05/12/2010  Created this file. :-)
15 //
16
17 #include "commandwidget.h"
18
19 #include "commands.h"
20 #include "qg_actionhandler.h"
21
22 CommandWidget::CommandWidget(QWidget * parent/*= 0*/, Qt::WindowFlags flags/*= 0*/):
23         QWidget(parent, flags),  actionHandler(NULL)
24 {
25         ui.setupUi(this);
26
27         //errStream = NULL;
28         ui.leCommand->setFrame(false);
29         ui.leCommand->setFocusPolicy(Qt::StrongFocus);
30         //setNormalMode();
31 }
32
33 CommandWidget::~CommandWidget()
34 {
35 }
36
37 bool CommandWidget::checkFocus()
38 {
39         return ui.leCommand->hasFocus();
40 }
41
42 void CommandWidget::setFocus()
43 {
44         //setCommandMode();
45         ui.leCommand->setFocus();
46 }
47
48 void CommandWidget::setCommand(const QString & cmd)
49 {
50         if (cmd != "")
51                 ui.lCommand->setText(cmd);
52         else
53                 ui.lCommand->setText(tr("Command:"));
54
55         ui.leCommand->setText("");
56 }
57
58 void CommandWidget::appendHistory(const QString & msg)
59 {
60         ui.teHistory->append(msg);
61 }
62
63 void CommandWidget::trigger()
64 {
65         QString cmd = ui.leCommand->text();
66
67         if (cmd == "")
68                 cmd = "\n";
69         else
70                 appendHistory(cmd);
71
72         if (actionHandler != NULL)
73                 actionHandler->command(cmd);
74
75         ui.leCommand->setText("");
76 }
77
78 void CommandWidget::tabPressed()
79 {
80         if (actionHandler != NULL)
81         {
82                 QStringList reducedChoice;
83                 QString typed = ui.leCommand->text();
84                 QStringList choice;
85
86                 // check current command:
87                 choice = actionHandler->getAvailableCommands();
88
89                 if (choice.count() == 0)
90                         choice = RS_COMMANDS->complete(typed);
91
92                 for(QStringList::Iterator it=choice.begin(); it!=choice.end(); ++it)
93                 {
94                         if (typed.isEmpty() || (*it).startsWith(typed))
95                                 reducedChoice << (*it);
96                 }
97
98                 // command found:
99                 if (reducedChoice.count() == 1)
100                         ui.leCommand->setText(reducedChoice.first());
101                 else if (reducedChoice.count() > 0)
102                         appendHistory(reducedChoice.join(", "));
103         }
104 }
105
106 void CommandWidget::escape()
107 {
108         //leCommand->clearFocus();
109
110         if (actionHandler != NULL)
111                 actionHandler->slotFocusNormal();
112 }
113
114 /*void CommandWidget::cmdChanged(const QString& text) {
115         // three equal letters enable hotkeys and move the focus away from the command line:
116         if (text.length()==3) {
117                 if (text.at(0)==text.at(1) && text.at(0)==text.at(2)) {
118                         escape();
119                 }
120         }
121 }*/
122
123 void CommandWidget::setActionHandler(QG_ActionHandler * ah)
124 {
125         actionHandler = ah;
126 }
127
128 void CommandWidget::setCommandMode()
129 {
130 //      ui.lCommand->setPaletteForegroundColor(Qt::blue);
131         QPalette palette;
132         palette.setColor(ui.lCommand->foregroundRole(), Qt::blue);
133         ui.lCommand->setPalette(palette);
134 }
135
136 void CommandWidget::setNormalMode()
137 {
138 //      ui.lCommand->setPaletteForegroundColor(Qt::black);
139         QPalette palette;
140         palette.setColor(ui.lCommand->foregroundRole(), Qt::black);
141         ui.lCommand->setPalette(palette);
142 }