diff --git a/companion/src/modeledit/mixerslist.cpp b/companion/src/modeledit/mixerslist.cpp index a40e4b600..da9292cfe 100644 --- a/companion/src/modeledit/mixerslist.cpp +++ b/companion/src/modeledit/mixerslist.cpp @@ -8,9 +8,8 @@ MixersList::MixersList(QWidget *parent, bool expo) : setContextMenuPolicy(Qt::CustomContextMenu); setSelectionMode(QAbstractItemView::SingleSelection); setDragEnabled(true); - //setDragDropMode(QAbstractItemView::InternalMove); - viewport()->setAcceptDrops(true); setDropIndicatorShown(true); + viewport()->setAcceptDrops(true); setItemDelegate(new MixersDelegate(parent)); } @@ -21,34 +20,31 @@ void MixersList::keyPressEvent(QKeyEvent *event) bool MixersList::dropMimeData( int index, const QMimeData * data, Qt::DropAction action ) { - qDebug() << "MixersList::dropMimeData() " << index << data->formats(); + // qDebug() << "MixersList::dropMimeData() index:" << index << "formats" << data->formats(); QByteArray dropData = data->data("application/x-qabstractitemmodeldatalist"); QDataStream stream(&dropData, QIODevice::ReadOnly); QByteArray qba; - while (!stream.atEnd()) - { + while (!stream.atEnd()) { int r,c; QMap v; stream >> r >> c >> v; QList lsVars; lsVars = v.values(); - QString itemString = lsVars.at(0).toString(); + //QString itemString = lsVars.at(0).toString(); qba.append(lsVars.at(1).toByteArray().mid(1)); - qDebug() << "MixersList::dropMimeData() added data " << lsVars.count() << lsVars; - if(itemString.isEmpty()) {}; + // qDebug() << "MixersList::dropMimeData() added" << lsVars.count() << "items, data:" << lsVars; + + //if(itemString.isEmpty()) {}; } - qDebug() << "MixersList::dropMimeData() qba.length() " << qba.length(); - if(qba.length()>0) - { + if(qba.length()>0) { QMimeData *mimeData = new QMimeData; if (expo) mimeData->setData("application/x-companion-expo", qba); else mimeData->setData("application/x-companion-mix", qba); - qDebug() << "MixersList::dropMimeData() emit mimeDropped " << index << mimeData << action; emit mimeDropped(index, mimeData, action); } @@ -56,45 +52,26 @@ bool MixersList::dropMimeData( int index, const QMimeData * data, Qt::DropAction return true; } - -void MixersList::mousePressEvent(QMouseEvent *event) -{ - qDebug() << "MixersList::mousePressEvent" ; - QListWidgetItem *item = currentItem(); - if (item) { - qDebug() << "MixersList::mousePressEvent" << item->text(); - } - if (event->button() == Qt::LeftButton) - { - //startPos = event->pos(); - } - QListWidget::mousePressEvent(event); - -} - - -#define ADDED_SPACE 20 - -void MixersDelegate::paint(QPainter *painter, - const QStyleOptionViewItem &option, - const QModelIndex &index) const +void MixersDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { + //Custom paint handler + //we paint HTML formated text inside list item QStyleOptionViewItemV4 options = option; initStyleOption(&options, index); painter->save(); + //setup html document QTextDocument doc; - doc.setHtml(options.text); - doc.setDefaultFont(option.font); + SetupDocument(doc, options); options.text = ""; options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter); - if ( index.model()->data(index, Qt::UserRole + 2).toInt() > 0 ) { - //qDebug() << "MixersDelegate::sizeHint() detected channel head"; - painter->translate(options.rect.left(), options.rect.top() + ADDED_SPACE); - QRect clip(0, 0, options.rect.width(), options.rect.height() - ADDED_SPACE); + if ( index.model()->data(index, GroupHeaderRole).toInt() > 0 ) { + //paint with added space + painter->translate(options.rect.left(), options.rect.top() + MIX_ROW_HEIGHT_INCREASE); + QRect clip(0, 0, options.rect.width(), options.rect.height() - MIX_ROW_HEIGHT_INCREASE); doc.drawContents(painter, clip); } else { @@ -102,33 +79,43 @@ void MixersDelegate::paint(QPainter *painter, QRect clip(0, 0, options.rect.width(), options.rect.height()); doc.drawContents(painter, clip); } - painter->restore(); - } QSize MixersDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const { + //custom size handler + //returns needed size for each HTML formated list item QStyleOptionViewItemV4 options = option; initStyleOption(&options, index); + //setup html document QTextDocument doc; - doc.setHtml(options.text); - doc.setDefaultFont(option.font); - doc.setTextWidth(options.rect.width()); + SetupDocument(doc, options); //qDebug() << "MixersDelegate::sizeHint() UserRole-> " << index.model()->data(index, Qt::UserRole + 2); - int height = doc.size().height(); if ( index.model()->data(index, Qt::UserRole + 2).toInt() > 0 ) { //qDebug() << "MixersDelegate::sizeHint() detected channel head"; - height = doc.size().height() + ADDED_SPACE; + height = doc.size().height() + MIX_ROW_HEIGHT_INCREASE; } - //qDebug() << "MixersDelegate::sizeHint() options.rect " << options.rect; //qDebug() << "MixersDelegate::sizeHint() result " << QSize(doc.idealWidth(), height); - return QSize(doc.idealWidth(), height); +} + +void MixersDelegate::SetupDocument(QTextDocument & doc, const QStyleOptionViewItemV4 & options) const +{ + //setup html document + doc.setHtml(options.text); + doc.setDefaultFont(options.font); + doc.setTextWidth(options.rect.width()); + + //minimize margins (default margins look ugly) + QTextFrame *tf = doc.rootFrame(); + QTextFrameFormat tff = tf->frameFormat(); + tff.setMargin(0); + tf->setFrameFormat(tff); } \ No newline at end of file diff --git a/companion/src/modeledit/mixerslist.h b/companion/src/modeledit/mixerslist.h index 90cc0a1d9..ee5f9a9ba 100644 --- a/companion/src/modeledit/mixerslist.h +++ b/companion/src/modeledit/mixerslist.h @@ -3,6 +3,10 @@ #include + +#define MIX_ROW_HEIGHT_INCREASE 8 //how much space is added above mixer row (for new channel), if 0 space adding is disabled +const int GroupHeaderRole = (Qt::UserRole+2); //defines new user role for list items. If value is > 0, then space is added before that item + class MixersList : public QListWidget { Q_OBJECT @@ -22,21 +26,20 @@ class MixersList : public QListWidget QPoint dragStartPosition; bool expo; - protected: - void mousePressEvent(QMouseEvent *event); - }; class MixersDelegate : public QStyledItemDelegate { Q_OBJECT public: - inline MixersDelegate(QObject *parent) : QStyledItemDelegate(parent) {} + inline MixersDelegate(QObject *parent) : QStyledItemDelegate(parent) {}; void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const; + private: + void SetupDocument(QTextDocument & doc, const QStyleOptionViewItemV4 & options) const; + }; - #endif // MIXERSLIST_H diff --git a/companion/src/modeledit/mixes.cpp b/companion/src/modeledit/mixes.cpp index aa74bc21d..eeb2564a4 100644 --- a/companion/src/modeledit/mixes.cpp +++ b/companion/src/modeledit/mixes.cpp @@ -90,55 +90,30 @@ void MixesPanel::update() } } -#define MIX_ROW_HEIGHT_INCREASE 8 //how much space is added above mixer row (for new channel), if 0 space adding is disabled - bool MixesPanel::AddMixerLine(int dest, const MixData * md) { bool new_ch; QString str = getMixerText(dest, &new_ch); QListWidgetItem *itm = new QListWidgetItem(); QByteArray qba(1, (quint8)dest); - if (md) { - qba.append((const char*)md, sizeof(MixData)); - //qDebug() << "appended mixdata for dest: " << dest; - } + if (md) qba.append((const char*)md, sizeof(MixData)); itm->setData(Qt::UserRole, qba); #if MIX_ROW_HEIGHT_INCREASE > 0 if ((new_ch && (dest > 0)) || (dest < 0)) { //increase size of this row - //QFontMetrics * fm = new QFontMetrics(itm->font()); - //QRect rect = fm->boundingRect("C)"); - //itm->setSizeHint(QSize(-1, (rect.height() * (14+MIX_ROW_HEIGHT_INCREASE))/10)); - itm->setData(Qt::UserRole+2, 1); + itm->setData(GroupHeaderRole, 1); } #endif MixerlistWidget->addItem(itm); itm->setText(str); - // if ((new_ch && (dest > 0)) || (dest < 0)) { - // MixerlistWidget->setItemWidget(itm, getMixerWidget(str)); - // } //qDebug() << "MixesPanel::AddMixerLine(): " << str; return new_ch; } -QWidget * MixesPanel::getMixerWidget(const QString & mixer_text) -{ - QLabel * l = new QLabel(); - l->setFont(QFont("Courier New",12)); - l->setTextFormat(Qt::RichText); - l->setAlignment(Qt::AlignBottom); - QString formated_str(mixer_text); - formated_str.replace(" ", " "); - l->setText(formated_str); - l->setAcceptDrops(true); - //l->setAutoFillBackground(true); - return l; -} - QString MixesPanel::getMixerText(int dest, bool * new_ch) { QString str; - *new_ch = 0; + if (new_ch) *new_ch = 0; if (dest < 0) { str = getChannelLabel(-dest); //highlight channell if needed @@ -152,12 +127,12 @@ QString MixesPanel::getMixerText(int dest, bool * new_ch) str = getChannelLabel(md->destCh); if ((dest == 0) || (model.mixData[dest-1].destCh != md->destCh)) { - *new_ch = 1; + if (new_ch) *new_ch = 1; //highlight channell if needed if (md->destCh == highlightedSource) { str = "" + str + ""; } - } + } else { str.fill(' '); } @@ -358,7 +333,7 @@ void MixesPanel::mixersCopy() void MixesPanel::pasteMixerMimeData(const QMimeData * mimeData, int destIdx) { - qDebug() << "void MixesPanel::pasteMixerMimeData" << mimeData->formats(); + //qDebug() << "void MixesPanel::pasteMixerMimeData" << mimeData->formats(); if(mimeData->hasFormat("application/x-companion-mix")) { int idx; // mixer index int dch; @@ -438,9 +413,7 @@ void MixesPanel::mixerHighlight() // qDebug() << "MixesPanel::mixerHighlight(): " << highlightedSource ; for(int i=0; icount(); i++) { int t = MixerlistWidget->item(i)->data(Qt::UserRole).toByteArray().at(0); - bool dummy; - //QWidget *w = getMixerWidget(); - MixerlistWidget->item(i)->setText(getMixerText(t, &dummy)); + MixerlistWidget->item(i)->setText(getMixerText(t, 0)); } } @@ -495,7 +468,7 @@ void MixesPanel::mixerlistWidget_customContextMenuRequested(QPoint pos) void MixesPanel::mimeMixerDropped(int index, const QMimeData *data, Qt::DropAction /*action*/) { int idx= MixerlistWidget->item(index > 0 ? index-1 : 0)->data(Qt::UserRole).toByteArray().at(0); - qDebug() << "MixesPanel::mimeMixerDropped()" << index << data; + //qDebug() << "MixesPanel::mimeMixerDropped()" << index << data; pasteMixerMimeData(data, idx); } diff --git a/companion/src/modeledit/mixes.h b/companion/src/modeledit/mixes.h index c1f54e601..dcf3e8052 100644 --- a/companion/src/modeledit/mixes.h +++ b/companion/src/modeledit/mixes.h @@ -55,7 +55,6 @@ class MixesPanel : public ModelPanel QString getChannelLabel(int curDest); bool AddMixerLine(int dest, const MixData * md = 0); QString getMixerText(int dest, bool * new_ch); - QWidget * getMixerWidget(const QString & mixer_text); }; #endif // MIXES_H