~ubuntu-branches/ubuntu/trusty/krusader/trusty

« back to all changes in this revision

Viewing changes to krusader/GUI/kcmdline.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-08-08 13:47:36 UTC
  • mfrom: (1.2.19 upstream)
  • Revision ID: james.westby@ubuntu.com-20110808134736-8e630ivgd2c3sgg5
Tags: 1:2.4.0~beta1-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
59
59
#include "../ActionMan/addplaceholderpopup.h"
60
60
#include "kcmdmodebutton.h"
61
61
 
 
62
 
 
63
CmdLineCombo::CmdLineCombo(QWidget *parent) : KHistoryComboBox(parent), _handlingLineEditResize(false)
 
64
{
 
65
    lineEdit()->installEventFilter(this);
 
66
    _pathLabel = new QLabel(this);
 
67
    _pathLabel->setWhatsThis(i18n("Name of directory where command will be processed."));
 
68
    _pathLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
 
69
}
 
70
 
 
71
bool CmdLineCombo::eventFilter(QObject *watched, QEvent *e)
 
72
{
 
73
    if(watched == lineEdit() && (e->type() == QEvent::Move || e->type() == QEvent::Resize)) {
 
74
        if(!_handlingLineEditResize) { // avoid infinite recursion
 
75
            _handlingLineEditResize = true;
 
76
            updateLineEditGeometry();
 
77
            _handlingLineEditResize = false;
 
78
        }
 
79
    }
 
80
    return false;
 
81
}
 
82
 
 
83
void CmdLineCombo::setPath(QString path)
 
84
{
 
85
    _path = path;
 
86
    doLayout();
 
87
}
 
88
 
 
89
void CmdLineCombo::updateLineEditGeometry()
 
90
{
 
91
    QRect r = lineEdit()->geometry();
 
92
    r.setLeft(_pathLabel->geometry().right());
 
93
    lineEdit()->setGeometry(r);
 
94
}
 
95
 
 
96
void CmdLineCombo::doLayout()
 
97
{
 
98
    QString pathNameLabel = _path;
 
99
    QFontMetrics fm(_pathLabel->fontMetrics());
 
100
    int textWidth = fm.width(_path);
 
101
    int maxWidth = (width() + _pathLabel->width()) * 2 / 5;
 
102
    int letters = _path.length() / 2;
 
103
 
 
104
    while (letters && textWidth > maxWidth) {
 
105
        pathNameLabel = _path.left(letters) + "..." + _path.right(letters);
 
106
        letters--;
 
107
        textWidth = fm.width(pathNameLabel);
 
108
    }
 
109
 
 
110
    _pathLabel->setText(pathNameLabel + "> ");
 
111
    _pathLabel->adjustSize();
 
112
 
 
113
    QStyleOptionComboBox opt;
 
114
    initStyleOption(&opt);
 
115
    QRect labelRect = style()->subControlRect(QStyle::CC_ComboBox, &opt,
 
116
                                            QStyle::SC_ComboBoxEditField, this);
 
117
    labelRect.adjust(2, 0, 0, 0);
 
118
    labelRect.setWidth(_pathLabel->width());
 
119
    _pathLabel->setGeometry(labelRect);
 
120
 
 
121
    updateLineEditGeometry();
 
122
}
 
123
 
 
124
void CmdLineCombo::resizeEvent(QResizeEvent *e)
 
125
{
 
126
    KHistoryComboBox::resizeEvent(e);
 
127
    doLayout();
 
128
}
 
129
 
 
130
void CmdLineCombo::keyPressEvent(QKeyEvent *e)
 
131
{
 
132
    switch (e->key()) {
 
133
    case Qt::Key_Enter:
 
134
    case Qt::Key_Return:
 
135
        if (e->modifiers() & Qt::ControlModifier) {
 
136
            SLOTS->insertFileName((e->modifiers() & Qt::ShiftModifier) != 0);
 
137
            break;
 
138
        }
 
139
        KHistoryComboBox::keyPressEvent(e);
 
140
        break;
 
141
    case Qt::Key_Down:
 
142
        if (e->modifiers()  == (Qt::ControlModifier | Qt::ShiftModifier)) {
 
143
            MAIN_VIEW->focusTerminalEmulator();
 
144
            return;
 
145
        } else
 
146
            KHistoryComboBox::keyPressEvent(e);
 
147
        break;
 
148
    case Qt::Key_Up:
 
149
        if (e->modifiers() == Qt::ControlModifier || e->modifiers() == (Qt::ControlModifier | Qt::ShiftModifier)) {
 
150
            emit returnToPanel();
 
151
            return;
 
152
        } else
 
153
            KHistoryComboBox::keyPressEvent(e);
 
154
        break;
 
155
    case Qt::Key_Escape:
 
156
        if (e->modifiers() == 0) {
 
157
            emit returnToPanel();
 
158
            return;
 
159
        } else
 
160
            KHistoryComboBox::keyPressEvent(e);
 
161
        break;
 
162
    default:
 
163
        KHistoryComboBox::keyPressEvent(e);
 
164
    }
 
165
}
 
166
 
 
167
 
62
168
KCMDLine::KCMDLine(QWidget *parent) : QWidget(parent)
63
169
{
64
170
    QGridLayout * layout = new QGridLayout(this);
65
171
    layout->setSpacing(0);
66
172
    layout->setContentsMargins(0, 0, 0, 0);
67
 
    path = new QLabel(this);
68
 
    path->setWhatsThis(i18n("Name of directory where command will be processed."));
69
 
    path->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
70
 
    path->setFrameStyle(QFrame::Box | QFrame::Sunken);
71
 
    path->setLineWidth(1);
72
 
    path->setFont(KGlobalSettings::generalFont());
 
173
 
73
174
    int height = QFontMetrics(KGlobalSettings::generalFont()).height();
74
175
    height =  height + 5 * (height > 14) + 6;
75
 
    path->setMaximumHeight(height);
76
 
    path->setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred));
77
 
    layout->addWidget(path, 0, 0);
78
176
 
79
177
    // and editable command line
80
178
    completion.setMode(KUrlCompletion::FileCompletion);
81
 
    cmdLine = new KrHistoryCombo(this);
 
179
    cmdLine = new CmdLineCombo(this);
82
180
    cmdLine->setMaxCount(100);  // remember 100 commands
83
181
    cmdLine->setMinimumContentsLength(10);
84
182
    cmdLine->setDuplicatesEnabled(false);
132
230
    this->addText(exp);
133
231
}
134
232
 
135
 
void KCMDLine::setCurrent(const QString &p)
 
233
void KCMDLine::setCurrent(const QString &path)
136
234
{
137
 
    pathName = p;
138
 
    calcLabelSize();
 
235
    cmdLine->setPath(path);
139
236
 
140
 
    completion.setDir(p);
 
237
    completion.setDir(path);
141
238
    // make sure our command is executed in the right directory
142
239
    // This line is important for Krusader overall functions -> do not remove !
143
 
    QDir::setCurrent(p);
144
 
}
145
 
 
146
 
void KCMDLine::calcLabelSize()
147
 
{
148
 
    QString pathNameLabel = pathName;
149
 
    QFontMetrics fm(path->fontMetrics());
150
 
    int textWidth = fm.width(pathName);
151
 
    int maxWidth = (cmdLine->width() + path->width()) * 2 / 5;
152
 
    int letters = pathName.length() / 2;
153
 
 
154
 
    while (letters && textWidth > maxWidth) {
155
 
        pathNameLabel = pathName.left(letters) + "..." + pathName.right(letters);
156
 
        letters--;
157
 
        textWidth = fm.width(pathNameLabel);
158
 
    }
159
 
 
160
 
    path->setText(pathNameLabel + '>');
 
240
    QDir::setCurrent(path);
161
241
}
162
242
 
163
243
void KCMDLine::slotRun()
175
255
        if (dir == "~")
176
256
            dir = QDir::homePath();
177
257
        else if (dir.left(1) != "/" && !dir.contains(":/"))
178
 
            dir = pathName + (pathName == "/" ? "" : "/") + dir;
 
258
            dir = cmdLine->path() + (cmdLine->path() == "/" ? "" : "/") + dir;
179
259
        SLOTS->refresh(dir);
180
260
    } else {
181
261
        exec();
211
291
 
212
292
QString KCMDLine::startpath() const
213
293
{
214
 
    return pathName;
 
294
    return cmdLine->path();
215
295
//     return path->text().left(path->text().length() - 1);
216
296
}
217
297
 
244
324
{
245
325
    cmdLine->lineEdit()->setText(text);
246
326
}
247
 
 
248
 
 
249
 
void KrHistoryCombo::keyPressEvent(QKeyEvent *e)
250
 
{
251
 
    switch (e->key()) {
252
 
    case Qt::Key_Enter:
253
 
    case Qt::Key_Return:
254
 
        if (e->modifiers() & Qt::ControlModifier) {
255
 
            SLOTS->insertFileName((e->modifiers() & Qt::ShiftModifier) != 0);
256
 
            break;
257
 
        }
258
 
        KHistoryComboBox::keyPressEvent(e);
259
 
        break;
260
 
    case Qt::Key_Down:
261
 
        if (e->modifiers()  == (Qt::ControlModifier | Qt::ShiftModifier)) {
262
 
            MAIN_VIEW->focusTerminalEmulator();
263
 
            return;
264
 
        } else
265
 
            KHistoryComboBox::keyPressEvent(e);
266
 
        break;
267
 
    case Qt::Key_Up:
268
 
        if (e->modifiers() == Qt::ControlModifier || e->modifiers() == (Qt::ControlModifier | Qt::ShiftModifier)) {
269
 
            emit returnToPanel();
270
 
            return;
271
 
        } else
272
 
            KHistoryComboBox::keyPressEvent(e);
273
 
        break;
274
 
    case Qt::Key_Escape:
275
 
        if (e->modifiers() == 0) {
276
 
            emit returnToPanel();
277
 
            return;
278
 
        } else
279
 
            KHistoryComboBox::keyPressEvent(e);
280
 
        break;
281
 
    default:
282
 
        KHistoryComboBox::keyPressEvent(e);
283
 
    }
284
 
}
285
 
 
286
 
#include "kcmdline.moc"
287