FilterChain.cpp 12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
    Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
    Copyright 2020 by Tomaz Canabrava <tcanabrava@gmail.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301  USA.
*/

#include "FilterChain.h"
#include "Filter.h"
23 24 25 26 27
#include "HotSpot.h"

#include "widgets/TerminalDisplay.h"

#include <QRect>
28
#include <QEvent>
29
#include <QKeyEvent>
30
#include <QPainter>
31
#include <QDebug>
32

33 34
#include <algorithm>

35
using namespace Konsole;
36
FilterChain::FilterChain(TerminalDisplay *terminalDisplay)
37 38 39 40
  : _terminalDisplay(terminalDisplay),
    _showUrlHint(false),
    _reverseUrlHints(false),
    _urlHintsModifiers(Qt::NoModifier)
41 42 43
{

}
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
FilterChain::~FilterChain()
{
    qDeleteAll(_filters);
}

void FilterChain::addFilter(Filter *filter)
{
    _filters.append(filter);
}

void FilterChain::removeFilter(Filter *filter)
{
    _filters.removeAll(filter);
}

void FilterChain::reset()
{
    for(auto *filter : _filters) {
        filter->reset();
    }
}

void FilterChain::setBuffer(const QString *buffer, const QList<int> *linePositions)
{
    for(auto *filter : _filters) {
        filter->setBuffer(buffer, linePositions);
    }
}

void FilterChain::process()
{
    for( auto *filter : _filters) {
        filter->process();
    }
}

void FilterChain::clear()
{
    _filters.clear();
}

QSharedPointer<HotSpot> FilterChain::hotSpotAt(int line, int column) const
{
    for(auto *filter : _filters) {
        QSharedPointer<HotSpot> spot = filter->hotSpotAt(line, column);
        if (spot != nullptr) {
           return spot;
        }
    }
    return nullptr;
}

QList<QSharedPointer<HotSpot>> FilterChain::hotSpots() const
{
    QList<QSharedPointer<HotSpot>> list;
    for (auto *filter : _filters) {
        list.append(filter->hotSpots());
   }
    return list;
}
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135


QRegion FilterChain::hotSpotRegion() const
{
    QRegion region;
    for (const auto &hotSpot : hotSpots()) {
        QRect r;
        r.setLeft(hotSpot->startColumn());
        r.setTop(hotSpot->startLine());
        if (hotSpot->startLine() == hotSpot->endLine()) {
            r.setRight(hotSpot->endColumn());
            r.setBottom(hotSpot->endLine());
            region |= _terminalDisplay->imageToWidget(r);
        } else {
            r.setRight(_terminalDisplay->columns());
            r.setBottom(hotSpot->startLine());
            region |= _terminalDisplay->imageToWidget(r);

            r.setLeft(0);

            for (int line = hotSpot->startLine() + 1 ; line < hotSpot->endLine(); line++) {
                r.moveTop(line);
                region |= _terminalDisplay->imageToWidget(r);
            }

            r.moveTop(hotSpot->endLine());
            r.setRight(hotSpot->endColumn());
            region |= _terminalDisplay->imageToWidget(r);
        }
    }
    return region;
}
136

Tomaz  Canabrava's avatar
Tomaz Canabrava committed
137
int FilterChain::count(HotSpot::Type type) const
138 139 140 141 142 143 144
{
    const auto hSpots = hotSpots();
    return std::count_if(std::begin(hSpots), std::end(hSpots),
            [type](const QSharedPointer<HotSpot> &s) {
                return s->type() == type;
            });
}
145

Tomaz  Canabrava's avatar
Tomaz Canabrava committed
146 147 148 149 150 151 152 153 154 155 156
QList<QSharedPointer<HotSpot>> FilterChain::filterBy(HotSpot::Type type) const
{
    QList<QSharedPointer<HotSpot>> hotspots;
    for (const auto &spot : hotSpots()) {
        if (spot->type() == type) {
            hotspots.append(spot);
        }
    }
    return hotspots;
}

157 158
void FilterChain::leaveEvent(TerminalDisplay *td, QEvent *ev)
{
159 160 161
    Q_UNUSED(td);
    Q_UNUSED(ev);
    _showUrlHint = false;
162 163
}

164 165
void FilterChain::keyReleaseEvent(TerminalDisplay *td, QKeyEvent *ev, int charLine, int charColumn)
{
166 167 168 169 170
    if (_showUrlHint) {
        _showUrlHint = false;
        td->update();
    }

171 172 173 174 175
    auto spot = hotSpotAt(charLine, charColumn);
    if (spot) {
        spot->keyReleaseEvent(td, ev);
    }
}
176

177 178 179 180
static int ascii_to_int(int c) {
    return c - '0';
}

181 182
void FilterChain::keyPressEvent(TerminalDisplay *td, QKeyEvent *ev, int charLine, int charColumn)
{
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    if ((_urlHintsModifiers != 0u) && ev->modifiers() == _urlHintsModifiers) {
        QList<QSharedPointer<HotSpot>> hotspots = filterBy(HotSpot::Link);
        int nHotSpots = hotspots.count();
        int hintSelected = ascii_to_int(ev->key());

        // Triggered a Hotspot via shortcut.
        if (hintSelected >= 0 && hintSelected < 10 && hintSelected < nHotSpots) {
            if (_reverseUrlHints) {
                hintSelected = nHotSpots - hintSelected - 1;
            }
            hotspots.at(hintSelected)->activate();
            _showUrlHint = false;
            td->update();
            return;
        }

        if (!_showUrlHint) {
            td->processFilters();
            _showUrlHint = true;
            td->update();
        }
    }

206 207 208
    auto spot = hotSpotAt(charLine, charColumn);
    if (spot) {
        spot->keyPressEvent(td, ev);
209 210
    }
}
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

void  FilterChain::mouseMoveEvent(TerminalDisplay *td, QMouseEvent *ev, int charLine, int charColumn)
{
    auto spot = hotSpotAt(charLine, charColumn);
    if (_hotSpotUnderMouse != spot) {
        if (_hotSpotUnderMouse) {
            _hotSpotUnderMouse->mouseLeaveEvent(td, ev);
        }
        _hotSpotUnderMouse = spot;
        if (_hotSpotUnderMouse) {
            _hotSpotUnderMouse->mouseEnterEvent(td, ev);
        }
    }

    if (spot) {
        spot->mouseMoveEvent(td, ev);
    }
228 229
}

230 231 232 233 234 235 236 237 238
void FilterChain::mouseReleaseEvent(TerminalDisplay *td, QMouseEvent *ev, int charLine, int charColumn)
{
    auto spot = hotSpotAt(charLine, charColumn);
    if (!spot) {
        return;
    }
    spot->mouseReleaseEvent(td, ev);
}

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
void FilterChain::paint(TerminalDisplay* td, QPainter& painter)
{
    // get color of character under mouse and use it to draw
    // lines for filters
    QPoint cursorPos = td->mapFromGlobal(QCursor::pos());
    int cursorLine;
    int cursorColumn;

    td->getCharacterPosition(cursorPos, cursorLine, cursorColumn, false);
    Character cursorCharacter = td->getCursorCharacter( std::min(cursorColumn, td->columns() - 1), cursorLine);
    painter.setPen(QPen(cursorCharacter.foregroundColor.color(td->colorTable())));

    // iterate over hotspots identified by the display's currently active filters
    // and draw appropriate visuals to indicate the presence of the hotspot

    const auto spots = hotSpots();
    int urlNumber;
256
    int urlNumInc;
257 258

    // TODO: Remove _reverseUrllHints from TerminalDisplay.
259 260 261 262 263
    // TODO: Access reverseUrlHints from the profile, here.
    if (_reverseUrlHints) {
        // The URL hint numbering should be 'physically' increasing on the
        // keyboard, so they start at one and end up on 10 (on 0, or at least
        // they used to, but it seems to have changed, I'll fix that later).
264
        urlNumber = count(HotSpot::Link);
265
        urlNumInc = -1;
266
    } else {
267
        urlNumber = 1;
268
        urlNumInc = 1;
269 270 271 272 273 274 275 276 277
    }

    for (const auto &spot : spots) {
        QRegion region;
        if (spot->type() == HotSpot::Link || spot->type() == HotSpot::EMailAddress || spot->type() == HotSpot::EscapedUrl) {
            QPair<QRegion, QRect> spotRegion = spot->region(td->fontWidth(), td->fontHeight(), td->columns(), td->contentRect());
            region = spotRegion.first;
            QRect r = spotRegion.second;

278 279
            // TODO: Move this paint code to HotSpot->drawHint();
            // TODO: Fix the Url Hints access from the Profile.
280 281 282 283 284 285 286 287 288 289
            if (_showUrlHint && spot->type() == HotSpot::Link) {
                if (urlNumber >= 0 && urlNumber < 10) {
                    // Position at the beginning of the URL
                    QRect hintRect(*region.begin());
                    hintRect.setWidth(r.height());
                    painter.fillRect(hintRect, QColor(0, 0, 0, 128));
                    painter.setPen(Qt::white);
                    painter.drawRect(hintRect.adjusted(0, 0, -1, -1));
                    painter.drawText(hintRect, Qt::AlignCenter, QString::number(urlNumber));
                }
290
                urlNumber += urlNumInc;
291 292 293 294 295 296 297
            }
        }

        if (spot->startLine() < 0 || spot->endLine() < 0) {
            qDebug() << "ERROR, invalid hotspot:";
            spot->debug();
        }
298

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
        for (int line = spot->startLine() ; line <= spot->endLine() ; line++) {
            int startColumn = 0;
            int endColumn = td->columns() - 1; // TODO use number of _columns which are actually
            // occupied on this line rather than the width of the
            // display in _columns

            // Check image size so _image[] is valid (see makeImage)
            if (endColumn >= td->columns() || line >= td->lines()) {
                break;
            }

            // ignore whitespace at the end of the lines
            while (td->getCursorCharacter(endColumn, line).isSpace() && endColumn > 0) {
                endColumn--;
            }

            // increment here because the column which we want to set 'endColumn' to
            // is the first whitespace character at the end of the line
            endColumn++;

            if (line == spot->startLine()) {
                startColumn = spot->startColumn();
            }
            if (line == spot->endLine()) {
                endColumn = spot->endColumn();
            }

            // TODO: resolve this comment with the new margin/center code
            // subtract one pixel from
            // the right and bottom so that
            // we do not overdraw adjacent
            // hotspots
            //
            // subtracting one pixel from all sides also prevents an edge case where
            // moving the mouse outside a link could still leave it underlined
            // because the check below for the position of the cursor
            // finds it on the border of the target area
            QRect r;
            r.setCoords(startColumn * td->fontWidth() + td->contentRect().left(),
                        line * td->fontHeight() + td->contentRect().top(),
                        endColumn * td->fontWidth() + td->contentRect().left() - 1,
                        (line + 1)* td->fontHeight() + td->contentRect().top() - 1);

            // Underline link hotspots
            // TODO: Fix accessing the urlHint here.
            // TODO: Move this code to UrlFilterHotSpot.
            const bool hasMouse = region.contains(td->mapFromGlobal(QCursor::pos()));
346
            if ((spot->type() == HotSpot::Link && _showUrlHint) || hasMouse) {
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
                QFontMetrics metrics(td->font());

                // find the baseline (which is the invisible line that the characters in the font sit on,
                // with some having tails dangling below)
                const int baseline = r.bottom() - metrics.descent();
                // find the position of the underline below that
                const int underlinePos = baseline + metrics.underlinePos();
                painter.drawLine(r.left() , underlinePos, r.right() , underlinePos);

                // Marker hotspots simply have a transparent rectangular shape
                // drawn on top of them
            } else if (spot->type() == HotSpot::Marker) {
                //TODO - Do not use a hardcoded color for this
                const bool isCurrentResultLine = (td->screenWindow()->currentResultLine() == (spot->startLine() + td->screenWindow()->currentLine()));
                QColor color = isCurrentResultLine ? QColor(255, 255, 0, 120) : QColor(255, 0, 0, 120);
                painter.fillRect(r, color);
            }
        }
    }
366
}
367

368 369 370 371 372 373 374 375 376
void FilterChain::setReverseUrlHints(bool value)
{
    _reverseUrlHints = value;
}

void FilterChain::setUrlHintsModifiers(Qt::KeyboardModifiers value)
{
    _urlHintsModifiers = value;
}