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