File: notebutton.cpp

package info (click to toggle)
portabase 2.0%2Bgit20110117-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 6,692 kB
  • sloc: cpp: 32,047; sh: 2,675; ansic: 2,320; makefile: 343; xml: 20; python: 16; asm: 10
file content (117 lines) | stat: -rw-r--r-- 2,864 bytes parent folder | download | duplicates (2)
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
/*
 * notebutton.cpp
 *
 * (c) 2002-2003,2009-2010 by Jeremy Bowman <jmbowman@alum.mit.edu>
 *
 * 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.
 */

/** @file notebutton.cpp
 * Source file for NoteButton
 */

#include <QFontMetrics>
#include <QRegExp>
#include "notebutton.h"
#include "noteeditor.h"
#include "portabase.h"

/**
 * Constructor.
 *
 * @param colName The name of the column this note belongs to
 * @param parent This button's parent widget
 */
NoteButton::NoteButton(const QString &colName, QWidget *parent)
#if defined(Q_WS_MAEMO_5)
    : QPushButton(parent), name(colName)
#else
    : QToolButton(parent), name(colName)
#endif
{
    setIcon(QIcon(":/icons/note.png"));
#if !defined(Q_WS_MAEMO_5)
    setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding,
                              QSizePolicy::Fixed, QSizePolicy::ToolButton));
#endif
    connect(this, SIGNAL(clicked()), this, SLOT(launchEditor()));
}

/**
 * Get the note's current content.
 *
 * @return The note text
 */
QString NoteButton::content()
{
    return noteContent;
}

/**
 * Set the note's content.
 *
 * @param text The note's new text
 */
void NoteButton::setContent(const QString &text)
{
    noteContent = text;
    QString buttonText(text);
    buttonText.replace(QRegExp("\n"), " ");
    QFontMetrics metrics = fontMetrics();
#if defined(Q_WS_MAEMO_5)
    int available = width() - 90 - metrics.width("...");
#else
    int available = width() - 60 - metrics.width("...");
#endif
    int length = buttonText.length();
    for (int i = 0; i < length; i++) {
        if (metrics.width(buttonText, i + 1) > available) {
            buttonText = buttonText.left(i) + "...";
            break;
        }
    }
    buttonText = buttonText.replace(QRegExp("&"), "&&");
    setText(buttonText);
}

/**
 * Launch the note content editor dialog.  Launched when the button is
 * clicked.
 */
void NoteButton::launchEditor()
{
    NoteEditor editor(name, false, this);
    editor.setContent(noteContent);
    if (editor.exec()) {
        setContent(editor.content());
    }
}

/**
 * Adjust the button's text accordingly when the button is resized.
 *
 * @param event A resize event for the button
 */
void NoteButton::resizeEvent(QResizeEvent *event)
{
  QAbstractButton::resizeEvent(event);
  if (isVisible()) {
      setContent(noteContent);
  }
}

/**
 * Set the button's text to the appropriate subset of the note content
 * before it is shown.
 *
 * @param event A widget show event for the button
 */
void NoteButton::showEvent(QShowEvent *event)
{
    QAbstractButton::showEvent(event);
    setContent(noteContent);
}