summaryrefslogtreecommitdiff
path: root/muse2/muse/widgets/header.cpp
blob: ef874fc4224f83bff93a9ee3181c58e0504ee953 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//=========================================================
//  MusE
//  Linux Music Editor
//    $Id: header.cpp,v 1.1.1.1 2003/10/27 18:55:05 wschweer Exp $
//  (C) Copyright 2000 Werner Schweer (ws@seh.de)
//=========================================================

#include "header.h"
#include "xml.h"
#include "popupmenu.h"

#include <QStringList>
#include <QStandardItemModel>
#include <QMouseEvent>

//---------------------------------------------------------
//   readStatus
//---------------------------------------------------------

void Header::readStatus(Xml& xml)
{

    for (;;) {
          Xml::Token token = xml.parse();
          const QString& tag = xml.s1();
          switch (token) {
                case Xml::Error:
                case Xml::End:
                      return;
                case Xml::Text:
                      {
                          QStringList l = tag.split(QString(" "), QString::SkipEmptyParts);
                          int index = count() -1;
                          for (QStringList::Iterator it = l.begin(); it != l.end(); ++it) {
                                int logialIdx=abs((*it).toInt());
                                bool isHidden = (*it).toInt() < 0 ? true:false;
                                int section = visualIndex(logialIdx);
                                moveSection(section, index);
                                if (isHidden)
                                  hideSection(logialIdx-1);
                                else
                                  showSection(logialIdx);
                                --index;
                          }

                          // loop again looking for missing indexes
                          for (int i =0; i < count(); i++) {
                              bool foundIt=false;
                              for (QStringList::Iterator it = l.begin(); it != l.end(); ++it) {
                                int id=((*it).toInt());
                                if ( id == i || i ==1-id )
                                    foundIt=true;
                              }
                              if (foundIt == false) {
                                int section = visualIndex(i);
                                moveSection(section, i);
                                //printf("Adding missing i %d index %d section %d!\n", i, index, section);
                              }
                          }
                      }
                      break;
                case Xml::TagStart:
                      xml.unknown("Header");
                      break;
                case Xml::TagEnd:
                      if (tag ==objectName())
                            return;
                default:
                      break;
                }
          }
}

//---------------------------------------------------------
//   writeStatus
//---------------------------------------------------------

void Header::writeStatus(int level, Xml& xml) const
      {
      //xml.nput(level, "<%s> ", name());
      xml.nput(level, "<%s> ", Xml::xmlString(objectName()).toLatin1().constData());
      int n = count();
      for (int i = n; i >= 0; --i) {
            if (isSectionHidden(logicalIndex(i)))
              xml.nput("%d ", -logicalIndex(i)-1); // hidden is stored as negative value starting from -1
            else
              xml.nput("%d ", logicalIndex(i));
          }
      //xml.put("</%s>", name());
      xml.put("</%s>", Xml::xmlString(objectName()).toLatin1().constData());
      }

//---------------------------------------------------------
//   Header
//---------------------------------------------------------

Header::Header(QWidget* parent, const char* name)
  : QHeaderView(Qt::Horizontal, parent) 
      {
      setObjectName(name);
      itemModel = new QStandardItemModel;
      setModel(itemModel);
      setDefaultSectionSize(30);

      }

//---------------------------------------------------------
//   setColumnLabel
//---------------------------------------------------------

void Header::setColumnLabel(const QString & text, int col, int width )
      {
      //printf("column set to %s %d %d \n", text.toLatin1().data(), col, width);
      QStandardItem *sitem = new QStandardItem(text );
      itemModel->setHorizontalHeaderItem(col, sitem);
      if (width > -1)
           resizeSection(col, width);
      }

//---------------------------------------------------------
//   setToolTip
//---------------------------------------------------------

void Header::setToolTip(int col, const QString &text)
      {
      QStandardItem *item = itemModel->horizontalHeaderItem(col);
      item->setToolTip(text);
      }

//---------------------------------------------------------
//   setWhatsThis
//---------------------------------------------------------

void Header::setWhatsThis(int col, const QString &text)
      {
      QStandardItem *item = itemModel->horizontalHeaderItem(col);
      item->setWhatsThis(text);
      }

void Header::mousePressEvent ( QMouseEvent * e )
{
  if (e->button() == Qt::RightButton) {

    PopupMenu* p = new PopupMenu();
    p->disconnect();
    p->clear();
    p->setTitle(tr("Track Info Columns"));
    QAction* act = 0;

    for(int i=0; i < count(); i++) {
      act = p->addAction(itemModel->horizontalHeaderItem(logicalIndex(i))->text() +
                         "\t - "+ itemModel->horizontalHeaderItem(logicalIndex(i))->toolTip());

      act->setCheckable(true);
      act->setChecked(!isSectionHidden(logicalIndex(i)));
      int data = logicalIndex(i);
      act->setData(data);
    }
    connect(p, SIGNAL(triggered(QAction*)), SLOT(changeColumns(QAction*)));
    p->exec(QCursor::pos());

    delete p;
    return;
  }

  QHeaderView::mousePressEvent(e);

}
void Header::changeColumns(QAction *a)
{
  int section = a->data().toInt();
  if (isSectionHidden(section))
    showSection(section);
  else
    hideSection(section);
}