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
|
/****************************************************************************
**
** Copyright (C) 2006-2009 fullmetalcoder <fullmetalcoder@hotmail.fr>
**
** This file is part of the Edyuk project <http://edyuk.org>
**
** This file may be used under the terms of the GNU General Public License
** version 3 as published by the Free Software Foundation and appearing in the
** file GPL.txt included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
#include "qstatuspanel.h"
#include "utilsSystem.h"
/*!
\file qstatuspanel.cpp
\brief Implementation of the QStatusPanel class.
\see QStatusPanel
*/
#include "qeditor.h"
#include "qdocument.h"
#include "qdocumentline.h"
#include "qdocumentcursor.h"
#include "qdocumentcursor_p.h"
/*!
\ingroup widgets
@{
*/
/*!
\class QStatusPanel
\brief A panel that display some status informations
*/
QCE_AUTO_REGISTER(QStatusPanel)
/*!
\brief Constructor
*/
QStatusPanel::QStatusPanel(QWidget *p)
: QPanel(p), m_conflictSpot(0)
{
setObjectName("statusPanel");
setFixedHeight(fontMetrics().lineSpacing() + 4);
timer=0;
}
/*!
\brief Empty destructor
*/
QStatusPanel::~QStatusPanel()
{
}
/*!
*/
QString QStatusPanel::type() const
{
return "Status";
}
/*!
*/
void QStatusPanel::editorChange(QEditor *e)
{
if ( editor() )
{
disconnect( editor(), SIGNAL( cursorPositionChanged() ),
this , SLOT ( update() ) );
if(timer) {
disconnect(timer, SIGNAL(timeout()), this, SLOT(update()));
delete timer;
timer=0;
}
}
if ( e )
{
connect(e , SIGNAL( cursorPositionChanged() ),
this, SLOT ( update() ) );
if(e->displayModifyTime()){
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000);
}
}
}
/*!
*/
bool QStatusPanel::paint(QPainter *p, QEditor *e)
{
//qDebug("drawing status panel... [%i, %i, %i, %i]",
// geometry().x(),
// geometry().y(),
// geometry().width(),
// geometry().height());
static QPixmap _warn = QPixmap(getRealIconFile("warning")).scaledToHeight(16, Qt::SmoothTransformation);
static QPixmap _mod = QPixmap(getRealIconFile("document-save")).scaledToHeight(16, Qt::SmoothTransformation);
QString s;
QString spacing(" ");
int xpos = 10;
const QFontMetrics fm(fontMetrics());
const int ls = QDocument::getLineSpacing();
const int ascent = fm.ascent() + 3;
QDocumentCursorHandle* c = e->cursorHandle();
if (c) {
s = tr("Line: %1").arg(c->lineNumber() + 1);
s += spacing + tr("Column: %1").arg(c->visualColumnNumber());
if (c->hasSelection()) {
if (c->anchorLineNumber() == c->lineNumber())
s += spacing + tr("Selected: %1").arg(qAbs(c->anchorColumnNumber() - c->columnNumber()));
else {
QDocumentCursor ss = c->selectionStart(), se = c->selectionEnd();
int chars = ss.line().length() - ss.columnNumber() + se.columnNumber();
for (int i=ss.lineNumber()+1;i<se.lineNumber();i++)
chars += ss.document()->line(i).length();
s += spacing + tr("Selected: %1").arg(chars);
s += spacing + tr("Lines: %1").arg(se.lineNumber() - ss.lineNumber() + 1);
}
}
}
p->drawText(xpos, ascent, s);
xpos += fm.width(s) + 10;
bool displayModifyIcon = false; // we never need this, because it's displayed in the editor tab.
int sz = qMin(height(), _mod.height());
QString timeDiff;
if(e->displayModifyTime() && e->document()){
int lastMod = e->document()->lastModified().secsTo(QDateTime::currentDateTime());
timeDiff = tr("(%1 min %2 s ago)").arg(lastMod / 60).arg(lastMod % 60);
}
xpos += 10;
if ( displayModifyIcon && e->isContentModified() )
{
p->drawPixmap(xpos, (height() - sz) / 2, sz, sz, _mod);
xpos += sz;
xpos += 10;
if(e->displayModifyTime()) p->drawText(xpos, ascent, timeDiff);
} else {
xpos += sz + 10;
}
xpos += fm.width(timeDiff);
xpos += 20;
s = editor()->flag(QEditor::Overwrite) ? tr("OVERWRITE") : tr("INSERT");
p->drawText(xpos, ascent, s);
xpos += fm.width(s) + 10;
m_conflictSpot = 0;
if ( editor()->isInConflict() )
{
s = tr("Conflict");
int w = fm.width(s) + 30;
if ( xpos + w + _warn.width() < width() )
{
m_conflictSpot = width() - (w + _warn.width());
p->drawText(width() - w + 15, ascent, s);
p->drawPixmap(m_conflictSpot, (ls - _warn.height()) / 2 + 2, _warn);
} else if ( xpos + _warn.width() < width() ) {
m_conflictSpot = width() - _warn.width();
p->drawPixmap(m_conflictSpot, (ls - _warn.height()) / 2 + 2, _warn);
}
}
setFixedHeight(fontMetrics().lineSpacing() + 4);
if(!e->displayModifyTime() && timer){
disconnect(timer, SIGNAL(timeout()), this, SLOT(update()));
delete timer;
timer=0;
}
if(e->displayModifyTime() && !timer){
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000);
}
//QTimer::singleShot(1000, this, SLOT( update() ) );
return true;
}
/*!
*/
void QStatusPanel::mousePressEvent(QMouseEvent *e)
{
if ( !editor() || (e->button() != Qt::LeftButton) || !m_conflictSpot || e->x() < m_conflictSpot )
{
editor()->setFocus();
return;
}
editor()->save();
}
/*!
*/
void QStatusPanel::mouseReleaseEvent(QMouseEvent *e)
{
Q_UNUSED(e)
editor()->setFocus();
}
/*! @} */
|