summaryrefslogtreecommitdiffstats
path: root/utils/regtools/qeditor/utils.h
blob: 026466fae0744074bb37f99dbacdbfdbf79a6019 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2014 by Amaury Pouly
 *
 * 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 software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#ifndef AUX_H
#define AUX_H

#include <QEvent>
#include <QPaintEvent>
#include <QLineEdit>
#include <QValidator>
#include <QToolButton>
#include <QMenu>
#include <QHBoxLayout>
#include <QTextEdit>
#include <QTableWidget>
#include <QToolBar>
#include <QLabel>
#include <QHBoxLayout>
#include <QItemEditorCreatorBase>
#include <QStyledItemDelegate>
#include <QComboBox>
#include <QFileDialog>
#include <QScrollBar>
#include <QGroupBox>
#include <QSortFilterProxyModel>
#include "settings.h"
#include "backend.h"

// FIXME see QTBUG-30392
namespace
{
template< typename T >
inline bool isUserType(const QVariant& var)
{
    return var.type() == QVariant::UserType && var.userType() == qMetaTypeId< T >();
}

}

class SocBitRangeValidator : public QValidator
{
    Q_OBJECT
public:
    SocBitRangeValidator(QObject *parent = 0);

    virtual void fixup(QString& input) const;
    virtual State validate(QString& input, int& pos) const;
    /* validate and return the interpreted value */
    State parse(const QString& input, int& last_bit, int& first_bit) const;
    /* create a valid string from range */
    QString generate(int last_bit, int first_bit) const;
    /* set maximum width in bits */
    void setWidth(int nr_bits);

protected:
    int m_width;
};

class SocFieldValidator : public QValidator
{
    Q_OBJECT
public:
    SocFieldValidator(QObject *parent = 0);
    SocFieldValidator(const soc_desc::field_t& field, QObject *parent = 0);

    virtual void fixup(QString& input) const;
    virtual State validate(QString& input, int& pos) const;
    /* validate and return the interpreted value */
    State parse(const QString& input, soc_word_t& val) const;

protected:
    soc_desc::field_t m_field;
};

class RegLineEdit : public QWidget
{
    Q_OBJECT
public:
    enum EditMode
    {
        Write, Set, Clear, Toggle
    };

    RegLineEdit(QWidget *parent = 0);
    ~RegLineEdit();
    void SetReadOnly(bool ro);
    void EnableSCT(bool en);
    void SetMode(EditMode mode);
    EditMode GetMode();
    QLineEdit *GetLineEdit();
    void setText(const QString& text);
    QString text() const;

    Q_PROPERTY(QString text READ text WRITE setText USER true)

protected slots:
    void OnWriteAct();
    void OnSetAct();
    void OnClearAct();
    void OnToggleAct();
protected:
    void ShowMode(bool show);
    void DoAutoHide();

    QHBoxLayout *m_layout;
    QToolButton *m_button;
    QLineEdit *m_edit;
    EditMode m_mode;
    bool m_has_sct;
    bool m_readonly;
    QMenu *m_menu;
};

class SocFieldItemDelegate : public QStyledItemDelegate
{
public:
    SocFieldItemDelegate(QObject *parent = 0):QStyledItemDelegate(parent), m_bitcount(32) {}
    SocFieldItemDelegate(const soc_desc::field_t& field, QObject *parent = 0)
        :QStyledItemDelegate(parent), m_bitcount(field.width) {}

    virtual QString displayText(const QVariant& value, const QLocale& locale) const;
    void setWidth(int bitcount);
protected:
    int m_bitcount;
};

class SocFieldEditor : public QLineEdit
{
    Q_OBJECT
    Q_PROPERTY(uint field READ field WRITE setField USER true)
public:
    SocFieldEditor(const soc_desc::field_t& field, QWidget *parent = 0);
    virtual ~SocFieldEditor();

    uint field() const;
    void setField(uint field);
    void SetRegField(const soc_desc::field_t& field);

signals:
    void editingFinished(uint value);

protected slots:
    void editDone();

protected:
    SocFieldValidator *m_validator;
    uint m_field;
    soc_desc::field_t m_reg_field;
};

class SocFieldEditorCreator : public QItemEditorCreatorBase
{
public:
    SocFieldEditorCreator() { m_field.pos = 0; m_field.width = 32; }
    SocFieldEditorCreator(const soc_desc::field_t& field):m_field(field) {}

    virtual QWidget *createWidget(QWidget *parent) const;
    virtual QByteArray valuePropertyName() const;

    void setWidth(int bitcount);

protected:
    soc_desc::field_t m_field;
};

class SocFieldCachedValue
{
public:
    SocFieldCachedValue():m_value(0) {}
    SocFieldCachedValue(const soc_desc::field_t& field, uint value);
    virtual ~SocFieldCachedValue() {}
    const soc_desc::field_t& field() const { return m_field; }
    uint value() const { return m_value; }
    /* return empty string if there no match */
    QString value_name() const { return m_name; }

    bool operator<(const SocFieldCachedValue& o) const;
protected:
    soc_desc::field_t m_field;
    uint m_value;
    QString m_name;
};

Q_DECLARE_METATYPE(SocFieldCachedValue)

class SocFieldBitRange
{
public:
    SocFieldBitRange():m_first_bit(0), m_last_bit(0) {}
    SocFieldBitRange(const soc_desc::field_t& field)
        :m_first_bit(field.pos), m_last_bit(field.pos + field.width - 1) {}
    SocFieldBitRange(int first, int last):m_first_bit(first), m_last_bit(last) {}
    unsigned GetFirstBit() const { return m_first_bit; }
    unsigned GetLastBit() const { return m_last_bit; }

    bool operator<(const SocFieldBitRange& o) const;
protected:
    unsigned m_first_bit, m_last_bit;
};

Q_DECLARE_METATYPE(SocFieldBitRange)

class SocFieldCachedItemDelegate : public QStyledItemDelegate
{
public:
    enum DisplayMode
    {
        DisplayValueAndName, /* "value (name)" or "value" if no name */
        DisplayName, /* "name" or "value" if no name */
        DisplayValue, /* "value" */
    };

    SocFieldCachedItemDelegate(QObject *parent = 0);
    virtual QString displayText(const QVariant& value, const QLocale& locale) const;
    void SetMode(DisplayMode mode) { m_mode = mode; }
    DisplayMode GetMode() const { return m_mode; }

protected:
    DisplayMode m_mode;
};

class SocFieldCachedEditor : public SocFieldEditor
{
    Q_OBJECT
    Q_PROPERTY(SocFieldCachedValue value READ value WRITE setValue USER true)
public:
    SocFieldCachedEditor(QWidget *parent = 0);
    virtual ~SocFieldCachedEditor();

    SocFieldCachedValue value() const;
    void setValue(SocFieldCachedValue field);
protected:
    SocFieldCachedValue m_value;
};

class SocFieldCachedEditorCreator : public QItemEditorCreatorBase
{
public:
    SocFieldCachedEditorCreator() {}

    virtual QWidget *createWidget(QWidget *parent) const;
    virtual QByteArray valuePropertyName() const;

protected:
};

struct RegThemeGroup
{
    QBrush foreground;
    QBrush background;
    QFont font;
};

struct RegTheme
{
    RegTheme():valid(false) {}
    bool valid;
    RegThemeGroup normal;
    RegThemeGroup diff;
    RegThemeGroup error;
};

class RegFieldTableModel : public QAbstractTableModel
{
    Q_OBJECT
public:
    RegFieldTableModel(QObject *parent);
    virtual int rowCount(const QModelIndex& parent = QModelIndex()) const;
    virtual int columnCount(const QModelIndex& parent = QModelIndex()) const;
    virtual QVariant data(const QModelIndex& index, int role) const;
    virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const;
    virtual Qt::ItemFlags flags(const QModelIndex& index) const;
    virtual bool setData(const QModelIndex& index, const QVariant& value, int role);

    void SetRegister(const soc_desc::register_t& reg);
    void UpdateRegister(const soc_desc::register_t& reg);
    soc_desc::register_t GetRegister() const;
    /* values can either be an invalid QVariant() (means no value/error), or a
     * QVariant containing a soc_word_t */
    void SetValues(const QVector< QVariant >& values);
    QVariant GetValue(int index);
    void SetTheme(const RegTheme& theme);
    void SetReadOnly(bool en);

signals:
    void OnValueModified(int index);

protected:
    void RecomputeTheme();

    enum
    {
        BitRangeColumn = 0,
        NameColumn,
        FirstValueColumn,
        DescColumnOffset = FirstValueColumn, /* offset from nr_values */
        ColumnCountOffset, /* offset from nr_values */
    };

    enum ColorStatus
    {
        None,
        Normal,
        Diff,
        Error
    };

    soc_desc::register_t m_reg;
    QVector< QVariant > m_value;
    QVector< ColorStatus > m_status;
    RegTheme m_theme;
    bool m_read_only;
};

class RegFieldProxyModel : public QSortFilterProxyModel
{
public:
    RegFieldProxyModel(QObject *parent):QSortFilterProxyModel(parent) {}
protected:
    bool lessThan(const QModelIndex& left, const QModelIndex& right) const;
};

class YRegDisplayItemDelegate : public QStyledItemDelegate
{
public:
    YRegDisplayItemDelegate(QObject *parent = 0);
    virtual void paint(QPainter * painter, const QStyleOptionViewItem& option,
        const QModelIndex & index) const;
    virtual QSize sizeHint(const QStyleOptionViewItem& option,
        const QModelIndex & index) const;
};

class YRegDisplay : public QAbstractItemView
{
    Q_OBJECT
public:
    YRegDisplay(QWidget *parent = 0);
    virtual QModelIndex indexAt(const QPoint& point) const;
    virtual void scrollTo(const QModelIndex& index, ScrollHint hint = EnsureVisible);
    virtual QRect visualRect(const QModelIndex& index) const;
    virtual void setModel(QAbstractItemModel *model);
    /* specify the number of bits to display */
    void setWidth(int nr_bits);
    /* returns the bit column at a point, or -1 if none except if closest=true */
    int bitColumnAt(const QPoint& point, bool closest = true) const;

protected slots:
    virtual void dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight);
    virtual void rowsInserted(const QModelIndex &parent, int start, int end);
    virtual void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
    virtual void scrollContentsBy(int dx, int dy);
    virtual void updateGeometries();

protected:
    int marginSize() const; // margin in cells
    int separatorSize() const; // size of lines betweens cells
    int minColumnWidth() const; // minimum width of a column (excluding separators)
    int maxColumnWidth() const; // maximum width of a column (excluding separators)
    int columnWidth(int col) const; // width of a 1-bit column (excluding separators)
    int headerHeight() const; // height of the header (excluding separators)
    int gapHeight() const; // height of gap between header and fields
    int maxContentHeight() const; // maximum height of field columns
    int headerTextSep() const; // height between digits in header
    int columnOffset(int col) const; // column offset
    int bitToColumn(int bit) const; // bit -> column
    void recomputeGeometry();
    QRect itemRect(const SocFieldBitRange& range, int col) const;
    QRect itemRect(const QModelIndex& index) const;

    virtual bool isIndexHidden(const QModelIndex& index) const;
    virtual QModelIndex moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers);
    virtual void setSelection(const QRect& rect, QItemSelectionModel::SelectionFlags flags);
    virtual int verticalOffset() const;
    virtual int horizontalOffset() const;
    virtual QRegion visualRegionForSelection(const QItemSelection& selection) const;
    virtual void paintEvent(QPaintEvent *event);
    virtual void resizeEvent(QResizeEvent* event);
    virtual bool viewportEvent(QEvent * event);

    bool m_is_dirty;
    int m_minimum_width, m_minimum_height;
    int m_range_col, m_data_col;
    int m_nr_bits;
    QModelIndex m_hover;
};

/**
 * The Qt designers chose to make QAbstractItemView a QAbstractScrollArea, so
 * that the table scrolls when it doesn't fit. This might be a problem when
 * one wants to put several tables on top of another, and the whole set into a
 * big scrollable area, because QAbstractScrollArea provides dummy values as
 * (minimum) size hints...So the big scroll area has no way of knowing the actual
 * size of the widget inside and it ends being a scrollable table inside another
 * scrollable, which is just super weird.
 * The Unscroll<T> class provides a workaround this behaviour: it expects T
 * to derive from QAbstractScrollArea and provides correct (minimum) size hints,
 * based on the value of the scroll bars.
 */
template<typename T>
class Unscroll : public T
{
public:
    Unscroll(QWidget *parent = 0):T(parent) {}
    virtual QSize sizeHint() const
    {
        QScrollBar *hsb = this->horizontalScrollBar();
        QScrollBar *vsb = this->verticalScrollBar();
        int w = hsb->maximum() - hsb->minimum() + hsb->pageStep();
        int h = vsb->maximum() - vsb->minimum() + vsb->pageStep();
        QMargins m = this->contentsMargins();
        return QSize(m.left() + w + m.right(), m.top() + h + m.bottom());
    }

    virtual QSize minimumSizeHint() const
    {
        return sizeHint();
    }
};

class GrowingTableView : public QTableView
{
    Q_OBJECT
public:
    GrowingTableView(QWidget *parent = 0);
    virtual void setModel(QAbstractItemModel *model);

protected slots:
    void DataChanged(const QModelIndex& tl, const QModelIndex& br);
};

class MyTextEditor : public QWidget
{
    Q_OBJECT
public:
    MyTextEditor(QWidget *parent = 0);
    void SetGrowingMode(bool en);
    void SetReadOnly(bool ro);
    void SetTextHtml(const QString& text);
    QString GetTextHtml();
    bool IsModified();

signals:
    void OnTextChanged();
    void OnTextChanged(const QString& text_html);

protected slots:
    void OnInternalTextChanged();
    void OnTextBold(bool checked);
    void OnTextItalic(bool checked);
    void OnTextUnderline(bool checked);
    void OnCharFormatChanged(const QTextCharFormat& fmt);

protected:
    virtual bool eventFilter(QObject *object, QEvent *event);

    bool m_growing_mode;
    bool m_read_only;
    QToolBar *m_toolbar;
    QTextEdit *m_edit;
    QToolButton *m_bold_button;
    QToolButton *m_italic_button;
    QToolButton *m_underline_button;
};

class BackendSelector : public QWidget
{
    Q_OBJECT
public:
    BackendSelector(Backend *backend, QWidget *parent = 0);
    virtual ~BackendSelector();
    IoBackend *GetBackend();
    void SetNothingMessage(const QString& msg);

signals:
    void OnSelect(IoBackend *backend);

protected:
    void ChangeBackend(IoBackend *new_backend);

    enum
    {
        DataSelNothing,
        DataSelFile,
#ifdef HAVE_HWSTUB
        DataSelDevice,
#endif
    };

    Backend *m_backend;
    IoBackend *m_io_backend;
    QComboBox *m_data_selector;
    QLineEdit *m_data_sel_edit;
#ifdef HAVE_HWSTUB
    QComboBox *m_dev_selector;
    HWStubBackendHelper m_hwstub_helper;
#endif
    QLabel *m_nothing_text;

private slots:
#ifdef HAVE_HWSTUB
    void OnDevListChanged();
    void OnDevChanged(int index);
    void OnDevListChanged2(bool, struct libusb_device *);
    void ClearDevList();
#endif
    void OnDataSelChanged(int index);
};

class MessageWidget : public QFrame
{
    Q_OBJECT
public:
    enum MessageType
    {
        Positive,
        Information,
        Warning,
        Error
    };

    MessageWidget(QWidget *parent = 0);
    virtual ~MessageWidget();
    /* returns message ID */
    int SetMessage(MessageType type, const QString& msg);
    /* clear message if ID match, nop otherwise */
    void HideMessage(int id);

protected:
    void UpdateType();

    QLabel *m_icon;
    QLabel *m_text;
    QToolButton *m_close;
    MessageType m_type;
    int m_id;

private slots:
    void OnClose(bool clicked);
};

Q_DECLARE_METATYPE(QModelIndex)

class YTabWidget : public QTabWidget
{
    Q_OBJECT
    Q_PROPERTY(bool tabOpenable READ tabOpenable WRITE setTabOpenable)
public:
    YTabWidget(QTabBar *tabbar = 0, QWidget *parent = 0);

    inline bool tabOpenable() const { return m_tab_openable; }
    void setTabOpenable(bool openable);
    void setTabOpenMenu(QMenu *menu);

signals:
    void tabOpenRequested();

protected slots:
    void OnOpenButton(bool checked);

protected:
    bool m_tab_openable;
    QToolButton *m_tab_open_button;
};

class Misc
{
public:
    static QGroupBox *EncloseInBox(const QString& name, QWidget *widget);
    static QGroupBox *EncloseInBox(const QString& name, QLayout *layout);
};

#endif /* AUX_H */