| 1 | /* |
| 2 | * File: CommandLine.cpp |
| 3 | * |
| 4 | * Author: Jacob Dekel |
| 5 | * Created on: Aug 7, 2009 |
| 6 | * |
| 7 | * Copyright (c) 2009-2013 Jacob Dekel |
| 8 | * $Id$ |
| 9 | * |
| 10 | * This program is free software: you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation, either version 3 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include "CommandLine.h" |
| 26 | #include "Preferences.h" |
| 27 | |
| 28 | #include <QKeyEvent> |
| 29 | |
| 30 | #include <iostream> |
| 31 | |
| 32 | CommandLine::CommandLine(QWidget * parent) |
| 33 | : QLineEdit(parent), mHistoryPtr(-1) |
| 34 | { |
| 35 | setMaxLength(512); |
| 36 | setFont(); |
| 37 | |
| 38 | } |
| 39 | |
| 40 | CommandLine::~CommandLine() |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | QSize CommandLine::sizeHint() |
| 45 | { |
| 46 | return QSize(1024, 50); |
| 47 | } |
| 48 | |
| 49 | void CommandLine::enterPressed(const QString & text1) |
| 50 | { |
| 51 | hOutDebug(4,"text1:"<< this->text().toStdString()); |
| 52 | mHistory.insert(mHistory.begin(),text1); |
| 53 | while (mHistory.size() > 100) |
| 54 | mHistory.erase(mHistory.end()-1); |
| 55 | mHistoryPtr = -1; |
| 56 | this->setText(""); |
| 57 | } |
| 58 | |
| 59 | void CommandLine::keyPressEvent(QKeyEvent * event) |
| 60 | { |
| 61 | hOutDebug(4, "key:" << event->key() << " " << (event->key() == Qt::Key_Up)); |
| 62 | QLineEdit::keyPressEvent(event); |
| 63 | switch (event->key()) |
| 64 | { |
| 65 | case Qt::Key_Enter: |
| 66 | case Qt::Key_Return: |
| 67 | enterPressed(this->text()); |
| 68 | break; |
| 69 | case Qt::Key_Down: |
| 70 | if (mHistoryPtr >= 0) mHistoryPtr--; |
| 71 | setLine(); |
| 72 | break; |
| 73 | case Qt::Key_Up: |
| 74 | mHistoryPtr++; |
| 75 | if (mHistoryPtr >= (signed) mHistory.size()) mHistoryPtr = mHistory.size()-1; |
| 76 | setLine(); |
| 77 | break; |
| 78 | case Qt::Key_C: |
| 79 | if (event->modifiers() && Qt::CTRL) |
| 80 | emit ctrl_c(); |
| 81 | break; |
| 82 | |
| 83 | default: |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void CommandLine::setLine() |
| 89 | { |
| 90 | hOutDebug(4,"setLine:" << mHistoryPtr << " size:" << mHistory.size() << " " << mHistory[mHistoryPtr].toStdString()); |
| 91 | if (mHistoryPtr < -1 || mHistoryPtr >= (signed) mHistory.size()) |
| 92 | return; |
| 93 | if (mHistoryPtr != -1) |
| 94 | this->setText(mHistory[mHistoryPtr]); |
| 95 | else |
| 96 | this->setText(""); |
| 97 | } |
| 98 | |
| 99 | void CommandLine::setFont() |
| 100 | { |
| 101 | Preferences& pref = Preferences::getInstance(); |
| 102 | QFont font(pref.fontName(Preferences::CommandFontObject).c_str(), |
| 103 | pref.fontSize(Preferences::CommandFontObject), |
| 104 | (pref.fontIsBold(Preferences::CommandFontObject) ? QFont::Bold : QFont::Normal), |
| 105 | pref.fontIsItalic(Preferences::CommandFontObject)); |
| 106 | |
| 107 | font.setStyleHint(QFont::Courier); |
| 108 | QLineEdit::setFont(font); |
| 109 | } |
| 110 | |
| 111 | bool CommandLine::empty() |
| 112 | { |
| 113 | return (!(mHistory.size() > 0)); |
| 114 | } |
| 115 | |
| 116 | void CommandLine::save() |
| 117 | { |
| 118 | Preferences& pref = Preferences::getInstance(); |
| 119 | while(mHistory.size() > 30) |
| 120 | mHistory.pop_back(); |
| 121 | pref.setHistory(mHistory); |
| 122 | } |
| 123 | |
| 124 | void CommandLine::restore() |
| 125 | { |
| 126 | Preferences& pref = Preferences::getInstance(); |
| 127 | pref.getHistory(mHistory); |
| 128 | |
| 129 | } |