Skip to content

Commit

Permalink
search side palette. See #52086
Browse files Browse the repository at this point in the history
  • Loading branch information
lasconic committed Aug 26, 2016
1 parent 7fe2bd3 commit b3c18ba
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 29 deletions.
120 changes: 93 additions & 27 deletions mscore/palette.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Palette::Palette(QWidget* parent)

Palette::~Palette()
{
foreach(PaletteCell* cell, cells)
for (PaletteCell* cell : cells)
delete cell;
}

Expand All @@ -113,6 +113,35 @@ void Palette::resizeEvent(QResizeEvent* e)
setFixedHeight(heightForWidth(e->size().width()));
}

//---------------------------------------------------------
// filter
/// return true if all filtered
//---------------------------------------------------------

bool Palette::filter(const QString& text)
{
QString t = text.toLower();
bool res = true;
for (PaletteCell* cell : cells) {
QStringList h = cell->name.toLower().split(" ");
bool c = false;
QStringList n = t.split(" ");
for (QString hs : h) {
for (QString ns : n)
c = hs.startsWith(ns);
if (c)
break;
}
bool contains = t.isEmpty() || c;
cell->visible = contains;
if (contains && res)
res = false;
}
setFixedHeight(heightForWidth(width()));
updateGeometry();
return res;
}

//---------------------------------------------------------
// setMoreElements
//---------------------------------------------------------
Expand Down Expand Up @@ -188,20 +217,20 @@ void Palette::contextMenuEvent(QContextMenuEvent* event)
QAction* moreAction = menu.addAction(tr("More Elements..."));
moreAction->setEnabled(_moreElements);

if (cells[i] && cells[i]->readOnly)
if (cellAt(i) && cellAt(i)->readOnly)
clearAction->setEnabled(false);

QAction* action = menu.exec(mapToGlobal(event->pos()));

if (action == clearAction) {
PaletteCell* cell = cells[i];
PaletteCell* cell = cellAt(i);
if (cell)
delete cell;
cells[i] = 0;
emit changed();
}
else if (action == contextAction) {
PaletteCell* c = cells[i];
PaletteCell* c = cellAt(i);
if (c == 0)
return;
PaletteCellProperties props(c);
Expand Down Expand Up @@ -245,8 +274,8 @@ void Palette::setGrid(int hh, int vv)

Element* Palette::element(int idx)
{
if (idx < size() && cells[idx])
return cells[idx]->element;
if (idx < size() && cellAt(idx))
return cellAt(idx)->element;
else
return 0;
}
Expand All @@ -268,7 +297,7 @@ void Palette::mousePressEvent(QMouseEvent* ev)
}
emit boxClicked(dragIdx);
}
PaletteCell* cell = cells[dragIdx];
PaletteCell* cell = cellAt(dragIdx);
if (cell && (cell->tag == "ShowMore"))
emit displayMore(_name);
}
Expand Down Expand Up @@ -317,8 +346,8 @@ void Palette::mouseDoubleClickEvent(QMouseEvent* ev)
return;

Element* element = 0;
if (i < size() && cells[i])
element = cells[i]->element;
if (i < size() && cellAt(i))
element = cellAt(i)->element;
if (element == 0)
return;

Expand Down Expand Up @@ -575,7 +604,7 @@ int Palette::idx(const QPoint& p) const
return -1;

int idx = row * nc + col;
if (idx < 0 || idx >= cells.size())
if (idx < 0 || idx >= size())
return -1;
return idx;
}
Expand Down Expand Up @@ -607,14 +636,14 @@ void Palette::mouseMoveEvent(QMouseEvent* ev)
{
if ((currentIdx != -1) && (dragIdx == currentIdx) && (ev->buttons() & Qt::LeftButton)
&& (ev->pos() - dragStartPosition).manhattanLength() > QApplication::startDragDistance()) {
PaletteCell* cell = cells[currentIdx];
PaletteCell* cell = cellAt(currentIdx);
if (cell && cell->element) {
QDrag* drag = new QDrag(this);
QMimeData* mimeData = new QMimeData;
Element* el = cell->element;
qreal mag = PALETTE_SPATIUM * extraMag / gscore->spatium();
QPointF spos = QPointF(dragStartPosition) / mag;
spos -= QPointF(cells[currentIdx]->x, cells[currentIdx]->y);
spos -= QPointF(cell->x, cell->y);

// DEBUG:
spos.setX(0.0);
Expand All @@ -638,7 +667,7 @@ void Palette::mouseMoveEvent(QMouseEvent* ev)
r = idxRect(currentIdx);
currentIdx = idx(ev->pos());
if (currentIdx != -1) {
if (cells[currentIdx] == 0)
if (cellAt(currentIdx) == 0)
currentIdx = -1;
else
r |= idxRect(currentIdx);
Expand Down Expand Up @@ -753,7 +782,7 @@ void Palette::paintEvent(QPaintEvent* /*event*/)

QColor bgColor(0xf6, 0xf0, 0xda);
if (preferences.fgUseColor)
bgColor = preferences.fgColor;
bgColor = preferences.fgColor;
#if 1
p.setBrush(bgColor);
p.drawRoundedRect(0, 0, width(), height(), 2, 2);
Expand Down Expand Up @@ -791,22 +820,26 @@ void Palette::paintEvent(QPaintEvent* /*event*/)
QPen pen(Qt::black);
pen.setWidthF(MScore::defaultStyle()->value(StyleIdx::staffLineWidth).toDouble() * PALETTE_SPATIUM * extraMag);

int actualIndex = 0;
for (int idx = 0; idx < cells.size(); ++idx) {
int yoffset = gscore->spatium() * _yOffset;
QRect r = idxRect(idx);
QRect r = idxRect(actualIndex);
QRect rShift = r.translated(0, yoffset);
p.setPen(pen);
QColor c(MScore::selectColor[0]);
if (idx == selectedIdx) {
if (actualIndex == selectedIdx) {
c.setAlpha(100);
p.fillRect(r, c);
}
else if (idx == currentIdx) {
else if (actualIndex == currentIdx) {
c.setAlpha(50);
p.fillRect(r, c);
}

if (cells.isEmpty() || cells[idx] == 0)
continue;
if (!cells[idx]->visible)
continue;

QString tag = cells[idx]->tag;
if (!tag.isEmpty()) {
Expand Down Expand Up @@ -837,8 +870,8 @@ void Palette::paintEvent(QPaintEvent* /*event*/)
p.drawPixmap(x + (hhgrid - size) / 2, y + (vgrid - size) / 2, pm);
}
else {
int row = idx / columns();
int column = idx % columns();
int row = actualIndex / columns();
int column = actualIndex % columns();

el->layout();
el->setPos(0.0, 0.0);
Expand Down Expand Up @@ -896,6 +929,8 @@ void Palette::paintEvent(QPaintEvent* /*event*/)
el->scanElements(&p, paintPaletteElement);
p.restore();
}
if (cells[idx]->visible)
actualIndex++;
}
}

Expand All @@ -922,10 +957,10 @@ bool Palette::event(QEvent* ev)
int idx = row * columns() + col;
if (idx >= cells.size())
return false;
if (cells[idx] == 0)
if (cellAt(idx) == 0)
return false;
QToolTip::showText(he->globalPos(),
qApp->translate("Palette", cells[idx]->name.toUtf8()), this);
qApp->translate("Palette", cellAt(idx)->name.toUtf8()), this);
return false;
}
return QWidget::event(ev);
Expand Down Expand Up @@ -1441,7 +1476,38 @@ int Palette::rows() const
int c = columns();
if (c == 0)
return 0;
return (cells.size() + c - 1) / c;
return (size() + c - 1) / c;
}

//---------------------------------------------------------
// size
//---------------------------------------------------------

int Palette::size() const
{
int s = 0;
for (PaletteCell* cell : cells) {
if (cell->visible)
s++;
}
return s;
}

//---------------------------------------------------------
// cellAt
//---------------------------------------------------------

PaletteCell* Palette::cellAt(int index)
{
int s = 0;
for (PaletteCell* cell : cells) {
if (cell->visible) {
if (s == index)
return cell;
s++;
}
}
return nullptr;
}

//---------------------------------------------------------
Expand All @@ -1453,10 +1519,10 @@ int Palette::heightForWidth(int w) const
int c = w / hgrid;
if (c <= 0)
c = 1;
int size = cells.size();
int s = size();
if (_moreElements)
size += 1;
int rows = (size + c - 1) / c;
s += 1;
int rows = (s + c - 1) / c;
if (rows <= 0)
rows = 1;
qreal mag = PALETTE_SPATIUM * extraMag;
Expand All @@ -1481,9 +1547,9 @@ QSize Palette::sizeHint() const
void Palette::actionToggled(bool /*val*/)
{
selectedIdx = -1;
int nn = cells.size();
int nn = size();
for (int n = 0; n < nn; ++n) {
Element* e = cells[n]->element;
Element* e = cellAt(n)->element;
if (e && e->type() == Element::Type::ICON) {
QAction* a = getAction(static_cast<Icon*>(e)->action());
if (a->isChecked()) {
Expand Down
5 changes: 4 additions & 1 deletion mscore/palette.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct PaletteCell {
double yoffset { 0.0 }; // in spatium units of "gscore"
qreal mag { 1.0 };
bool readOnly { false };
bool visible { true };
};

//---------------------------------------------------------
Expand Down Expand Up @@ -188,14 +189,16 @@ class Palette : public QWidget {
qreal yOffset() const { return _yOffset; }
int columns() const { return width() / hgrid; }
int rows() const;
int size() const { return cells.size(); }
int size() const;
PaletteCell* cellAt(int index);
void setCellReadOnly(int c, bool v) { cells[c]->readOnly = v; }
QString name() const { return _name; }
void setName(const QString& s) { _name = s; }
int gridWidth() const { return hgrid; }
int gridHeight() const { return vgrid; }
bool moreElements() const { return _moreElements; }
void setMoreElements(bool val);
bool filter(const QString& text);

virtual int heightForWidth(int) const;
virtual QSize sizeHint() const;
Expand Down
37 changes: 36 additions & 1 deletion mscore/palettebox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ PaletteBox::PaletteBox(QWidget* parent)
QVBoxLayout* vl = new QVBoxLayout(w);
vl->setMargin(0);
QHBoxLayout* hl = new QHBoxLayout;
hl->setContentsMargins(5,5,5,0);
hl->setContentsMargins(5,0,5,0);

workspaceList = new QComboBox;
hl->addWidget(workspaceList);
Expand All @@ -53,6 +53,14 @@ PaletteBox::PaletteBox(QWidget* parent)

setWidget(w);

searchBox = new QLineEdit(this);
searchBox->setPlaceholderText(tr("Filter"));
searchBox->setClearButtonEnabled(true);
connect(searchBox, SIGNAL(textChanged(const QString&)), this, SLOT(filterPalettes(const QString&)));
QHBoxLayout* hlSearch = new QHBoxLayout;
hlSearch->setContentsMargins(5,0,5,0);
hlSearch->addWidget(searchBox);

PaletteBoxScrollArea* sa = new PaletteBoxScrollArea;
sa->setFocusPolicy(Qt::NoFocus);
sa->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
Expand All @@ -62,6 +70,7 @@ PaletteBox::PaletteBox(QWidget* parent)
sa->setWidgetResizable(true);
sa->setFrameShape(QFrame::NoFrame);
vl->addWidget(sa);
vl->addLayout(hlSearch);
vl->addLayout(hl);

QWidget* paletteList = new QWidget;
Expand Down Expand Up @@ -92,6 +101,32 @@ void PaletteBox::retranslate()
updateWorkspaces();
}

//---------------------------------------------------------
// retransfilterPaletteslate
//---------------------------------------------------------

void PaletteBox::filterPalettes(const QString& text)
{
for (int i = 0; i < vbox->count(); i++) {
QWidgetItem* wi = static_cast<QWidgetItem*>(vbox->itemAt(i));
PaletteBoxButton* b = static_cast<PaletteBoxButton*>(wi->widget());
i++;
wi = static_cast<QWidgetItem*>(vbox->itemAt(i));
if (!wi) return;
Palette* p = static_cast<Palette*>(wi->widget());
bool f = p->filter(text);
b->setVisible(!f);
if (b->isVisible()) {
if (text.isEmpty())
b->showPalette(false);
else
b->showPalette(true);
}
else
b->showPalette(false);
}
}

//---------------------------------------------------------
// workspaceSelected
//---------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions mscore/palettebox.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class PaletteBox : public QDockWidget {
QVBoxLayout* vbox;
Palette* newPalette(const QString& name, int slot);
QComboBox* workspaceList;
QLineEdit* searchBox;
const int paletteStretch = 1000;
QAction* singlePaletteAction;
QToolButton* addWorkspaceButton;
Expand All @@ -43,6 +44,7 @@ class PaletteBox : public QDockWidget {
void workspaceSelected(int idx);
void newWorkspaceClicked();
void setSinglePalette(bool);
void filterPalettes(const QString& text);

signals:
void changed();
Expand Down

0 comments on commit b3c18ba

Please sign in to comment.