Skip to content
This repository has been archived by the owner on Dec 17, 2017. It is now read-only.

Commit

Permalink
Improvements in the razor-runner
Browse files Browse the repository at this point in the history
Added a customommand provider.
  • Loading branch information
SokoloffA committed Mar 29, 2012
1 parent 4f3659b commit 0f01689
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 88 deletions.
29 changes: 23 additions & 6 deletions razorqt-runner/commanditemmodel.cpp
Expand Up @@ -32,6 +32,7 @@
#include <QtCore/QFileInfo> #include <QtCore/QFileInfo>
#include <QtCore/QProcess> #include <QtCore/QProcess>
#include <QtCore/QDebug> #include <QtCore/QDebug>
#include <limits.h>




/************************************************ /************************************************
Expand Down Expand Up @@ -110,6 +111,12 @@ bool CommandItemModel::filterAcceptsRow(int sourceRow, const QModelIndex &/*sour
************************************************/ ************************************************/
bool CommandItemModel::lessThan(const QModelIndex &left, const QModelIndex &right) const bool CommandItemModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{ {
if (left == mSourceModel->customCommandIndex())
return true;

if (right == mSourceModel->customCommandIndex())
return false;

if (mOnlyHistory) if (mOnlyHistory)
return left.row() < right.row(); return left.row() < right.row();
else else
Expand All @@ -123,25 +130,29 @@ bool CommandItemModel::lessThan(const QModelIndex &left, const QModelIndex &righ
QModelIndex CommandItemModel::appropriateItem(const QString &pattern) const QModelIndex CommandItemModel::appropriateItem(const QString &pattern) const
{ {
QModelIndex res; QModelIndex res;
int delta = 0xFFFF; unsigned int rank = 0;


int cnt = rowCount(); int cnt = rowCount();

for (int i=0; i<cnt; ++i) for (int i=0; i<cnt; ++i)
{ {
QModelIndex ind = index(i,0); QModelIndex ind = index(i,0);
QModelIndex srcIndex = mapToSource(ind); QModelIndex srcIndex = mapToSource(ind);
if (srcIndex == mSourceModel->customCommandIndex())
continue;

This comment has been minimized.

Copy link
@amoskvin

amoskvin Sep 20, 2012

Member

Is the "continue" intentional? I would "return ind" here, so that it selects the matching command.

This comment has been minimized.

Copy link
@amoskvin

amoskvin Sep 23, 2012

Member

It definitely isn't the expected behavior, so I'll change it.

pic
pic

This comment has been minimized.

Copy link
@SokoloffA

SokoloffA Oct 10, 2012

Author Member

No, this not mistake. I wrote it consciously. I thought that founded .desktop entries have priority over commands in the $PATH. But now I'm not sure. On the one hand, if you write "libreoffice", you expected to start libreoffice. not libreoffice calc. On the other hand, many commands have a short names (rm, mv, cp), and your algorithm increases the rate of false positives. When you write "ed" I think, you want to start some GUI editor, but not ed.
Maybe some smarter? If text is shorter then 3 symbols (length is discussed), priority on .desktop, else priority on command.

This comment has been minimized.

Copy link
@amoskvin

amoskvin via email Oct 11, 2012

Member

This comment has been minimized.

Copy link
@SokoloffA

SokoloffA Oct 11, 2012

Author Member

I 've been thinking - the applink provider does not check the command, right?

Why? ApplinkItem check command. See AppLinkItem::rank.

It could be made to check if the command starts with what's typed in, and if so, give it a larger weight.

Algorithm pay attention on position.

unsigned int CommandProviderItem::stringRank(const QString str, const QString pattern) const
{
    int n = str.indexOf(pattern, 0, Qt::CaseInsensitive);
    if (n<0)
        return 0;

    return MAX_RANK - ((str.length() - pattern.length()) + n * 5);
}

If we type "term":

  • for "xterm" rank will be: 65535 - ((5 - 4) + 1 * 5) = 65529
  • for "gnome-terminal" - 65535 - ((14 - 4) + 6 * 5) = 65495

This would solve both problems I showed because if I type in "libr" it would match the LibreOffice entry (the only command that starts with that), and if I type "term" it would match "terminal" rather than "gnome-terminal". Additionally, it would be possible to search by command name, for instance "lowr" would produce "LibreOffice Writer" (lowriter).

Do you advise not show commands that contains text in the middle? I don't like it, I often search the programs only by parts. Like "creat" for qtcreator, or "server" for "rdp server" from history.

It still seems a bit "wrong" to me though, since traditionally launchers (in KDE3, Gnome2, Xfce) were expected to just launch the command that's typed in...

Razor-runner is more like a krunner.

This comment has been minimized.

Copy link
@amoskvin

amoskvin Oct 11, 2012

Member

I've been thinking - the applink provider does not check the
command, right?

Why? ApplinkItem check command. See AppLinkItem::rank.

OK, my bad. The menu entry actually starts "libreoffice --writer", not "lowriter" as I expected.

[...]

Do you advise not show commands that contains text in the middle? I
don't like it, I often search the programs only by parts. Like "creat"
for qtcreator, or "server" for "rdp server" from history.

No, what I was thinking is that those items whose command (not the title) matches at the beginning would have a higher rank than all others.

So "creat" should still match Qt Creator since you probably don't have any application (in menu) whose command starts with that. Same with "server".

With the current ranking algorithm, the difference between "terminal" and "xterm" when you type in "term" is only 3 - I think it should be higher. And when you compare gnome-terminal with xfce terminal, doesn't seem like it even takes the command into account since both of them are titled "Terminal" and it does qMax(stringRank(mCommand, pattern), stringRank(mTitle, pattern)).

I haven't played with the code yet, so I can't say any specifics.

It still seems a bit "wrong" to me though, since traditionally
launchers (in KDE3, Gnome2, Xfce) were expected to just launch the
command that's typed in...

Razor-runner is more like a krunner.

KRunner still launches the command when you type it in, though:

pic
pic

It actually does not start searching until there are at least 3 characters, so ed is the only result.

const CommandProviderItem *item = mSourceModel->command(srcIndex); const CommandProviderItem *item = mSourceModel->command(srcIndex);
if (!item) if (!item)
continue; continue;


int d = item->tile().indexOf(pattern, 0, Qt::CaseInsensitive); unsigned int r = item->rank(pattern);
if (d<delta) if (r > rank)
{ {
res = ind; res = ind;
delta = d; rank = r;
} }


if (delta==0) if (rank >= MAX_RANK)
break; break;
} }


Expand Down Expand Up @@ -169,8 +180,14 @@ void CommandItemModel::rebuild()
CommandSourceItemModel::CommandSourceItemModel(QObject *parent) : CommandSourceItemModel::CommandSourceItemModel(QObject *parent) :
QAbstractListModel(parent) QAbstractListModel(parent)
{ {
mCustomCommandProvider = new CustomCommandProvider;
mProviders.append(mCustomCommandProvider);
rebuild();
mCustomCommandIndex = index(0, 0);

mHistoryProvider = new HistoryProvider(); mHistoryProvider = new HistoryProvider();
mProviders.append(mHistoryProvider); mProviders.append(mHistoryProvider);

mProviders.append(new AppLinkProvider()); mProviders.append(new AppLinkProvider());
#ifdef MATH_ENABLED #ifdef MATH_ENABLED
mProviders.append(new MathProvider()); mProviders.append(new MathProvider());
Expand Down Expand Up @@ -230,7 +247,7 @@ QVariant CommandSourceItemModel::data(const QModelIndex &index, int role) const
switch (role) switch (role)
{ {
case Qt::DisplayRole: case Qt::DisplayRole:
return QString("<b>%1</b><br>\n%2\n").arg(item->tile(), item->comment()); return QString("<b>%1</b><br>\n%2\n").arg(item->title(), item->comment());


case Qt::DecorationRole: case Qt::DecorationRole:
return item->icon(); return item->icon();
Expand Down
9 changes: 9 additions & 0 deletions razorqt-runner/commanditemmodel.h
Expand Up @@ -50,12 +50,18 @@ class CommandSourceItemModel: public QAbstractListModel


void addHistoryCommand(const QString &command); void addHistoryCommand(const QString &command);


QString command() const { return mCustomCommandProvider->command(); }
void setCommand(const QString &command) { mCustomCommandProvider->setCommand(command); }

QPersistentModelIndex customCommandIndex() const { return mCustomCommandIndex; }
public slots: public slots:
void rebuild(); void rebuild();


private: private:
QList<CommandProvider*> mProviders; QList<CommandProvider*> mProviders;
HistoryProvider *mHistoryProvider; HistoryProvider *mHistoryProvider;
CustomCommandProvider *mCustomCommandProvider;
QPersistentModelIndex mCustomCommandIndex;
}; };




Expand All @@ -76,6 +82,9 @@ class CommandItemModel: public QSortFilterProxyModel
bool isShowOnlyHistory() const { return mOnlyHistory; } bool isShowOnlyHistory() const { return mOnlyHistory; }
void showOnlyHistory(bool onlyHistory) { mOnlyHistory = onlyHistory; } void showOnlyHistory(bool onlyHistory) { mOnlyHistory = onlyHistory; }


QString command() const { return mSourceModel->command(); }
void setCommand(const QString &command) { mSourceModel->setCommand(command); }

public slots: public slots:
void rebuild(); void rebuild();


Expand Down
14 changes: 6 additions & 8 deletions razorqt-runner/dialog.cpp
Expand Up @@ -68,13 +68,10 @@ Dialog::Dialog(QWidget *parent) :


connect(mSettings, SIGNAL(settingsChanged()), this, SLOT(applySettings())); connect(mSettings, SIGNAL(settingsChanged()), this, SLOT(applySettings()));



ui->commandEd->installEventFilter(this); ui->commandEd->installEventFilter(this);
ui->commandEd->setInsertPolicy(QComboBox::NoInsert);
ui->commandEd->setCompleter(0);


connect(ui->commandEd, SIGNAL(textChanged(QString)), this, SLOT(setFilter(QString))); connect(ui->commandEd, SIGNAL(textChanged(QString)), this, SLOT(setFilter(QString)));
connect(ui->commandEd->lineEdit(), SIGNAL(returnPressed()), this, SLOT(runCommand())); connect(ui->commandEd, SIGNAL(returnPressed()), this, SLOT(runCommand()));




mCommandItemModel = new CommandItemModel(this); mCommandItemModel = new CommandItemModel(this);
Expand Down Expand Up @@ -168,7 +165,7 @@ bool Dialog::editKeyPressEvent(QKeyEvent *event)
{ {
case Qt::Key_Up: case Qt::Key_Up:
case Qt::Key_PageUp: case Qt::Key_PageUp:
if (ui->commandEd->currentText().isEmpty() && if (ui->commandEd->text().isEmpty() &&
ui->commandList->isVisible() && ui->commandList->isVisible() &&
ui->commandList->currentIndex().row() == 0 ui->commandList->currentIndex().row() == 0
) )
Expand All @@ -181,7 +178,7 @@ bool Dialog::editKeyPressEvent(QKeyEvent *event)


case Qt::Key_Down: case Qt::Key_Down:
case Qt::Key_PageDown: case Qt::Key_PageDown:
if (ui->commandEd->currentText().isEmpty() && if (ui->commandEd->text().isEmpty() &&
ui->commandList->isHidden() ui->commandList->isHidden()
) )
{ {
Expand Down Expand Up @@ -313,6 +310,7 @@ void Dialog::setFilter(const QString &text, bool onlyHistory)
if (mCommandItemModel->isOutDated()) if (mCommandItemModel->isOutDated())
mCommandItemModel->rebuild(); mCommandItemModel->rebuild();


mCommandItemModel->setCommand(text);
mCommandItemModel->showOnlyHistory(onlyHistory); mCommandItemModel->showOnlyHistory(onlyHistory);
mCommandItemModel->setFilterWildcard(text); mCommandItemModel->setFilterWildcard(text);


Expand Down Expand Up @@ -346,7 +344,7 @@ void Dialog::runCommand()
} }
else else
{ {
QString command = ui->commandEd->currentText(); QString command = ui->commandEd->text();
res = QProcess::startDetached(command); res = QProcess::startDetached(command);
if (res) if (res)
{ {
Expand All @@ -357,7 +355,7 @@ void Dialog::runCommand()
if (res) if (res)
{ {
hide(); hide();
ui->commandEd->clearEditText(); ui->commandEd->clear();
} }


} }
Expand Down
11 changes: 1 addition & 10 deletions razorqt-runner/dialog.ui
Expand Up @@ -79,11 +79,7 @@
</widget> </widget>
</item> </item>
<item> <item>
<widget class="CommandComboBox" name="commandEd"> <widget class="QLineEdit" name="commandEd"/>
<property name="editable">
<bool>true</bool>
</property>
</widget>
</item> </item>
<item> <item>
<widget class="QToolButton" name="closeButton"> <widget class="QToolButton" name="closeButton">
Expand Down Expand Up @@ -146,11 +142,6 @@
<header>widgets.h</header> <header>widgets.h</header>
<container>1</container> <container>1</container>
</customwidget> </customwidget>
<customwidget>
<class>CommandComboBox</class>
<extends>QComboBox</extends>
<header>widgets.h</header>
</customwidget>
</customwidgets> </customwidgets>
<tabstops> <tabstops>
<tabstop>closeButton</tabstop> <tabstop>closeButton</tabstop>
Expand Down

0 comments on commit 0f01689

Please sign in to comment.