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
|
/*
This file is part of RoboJournal.
Copyright (c) 2012 by Will Kraft <pwizard@gmail.com>.
RoboJournal 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 3 of the License, or
(at your option) any later version.
RoboJournal is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RoboJournal. If not, see <http://www.gnu.org/licenses/>.
*/
#include "editor.h"
#include "ui_editor.h"
#include <iostream>
#include <buffer.h>
#include <stdlib.h>
#include <QDate>
#include "mysqlcore.h"
#include <QDateEdit>
#include <QMessageBox>
#include <QMenu>
#include <QFont>
#include <QStatusBar>
QString Editor::body;
QString Editor::title;
int Editor::day;
int Editor::month;
int Editor::year;
void Editor::reject(){
ConfirmExit();
}
// Ask the user to confirm the desire to exit the editor window. Triggered if ESC is pressed, Abort is clicked,
// or window is closed.
void Editor::ConfirmExit(){
if(Buffer::showwarnings){
QMessageBox m(this);
m.setWindowTitle("RoboJournal");
m.setIcon(QMessageBox::Question);
m.setText("This entry will not be saved if you close the editor now. Are you sure you want to do this?");
m.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
m.setDefaultButton(QMessageBox::No);
int choice=m.exec();
switch(choice){
case QMessageBox::Yes:
this->done(0);
break;
case QMessageBox::No:
// do nothing
break;
}
}
else{
this->done(0);
}
}
Editor::Editor(QWidget *parent) :
QDialog(parent),
ui(new Ui::Editor)
{
ui->setupUi(this);
// disable format options for now
ui->FormatButton->setDisabled(true);
// hide question mark button in title bar when running on Windows
this->setWindowFlags(this->windowFlags() & ~Qt::WindowContextHelpButtonHint);
if(Buffer::editmode){
InitiateEditMode();
LoadEntry(Buffer::editentry);
}
else{
// set todays date if we're writing a new entry
SetDate();
}
SetupMenu();
int width=this->width();
int height=this->height();
this->setMaximumSize(width,height);
this->setMinimumSize(width,height);
// Only zero everything out if we are creating a new entry. Otherwise, DocumentStats()
// gets called when the editor starts up in edit mode and the proper values are pre-loaded.
if(!Buffer::editmode){
ui->WordCount->setText(" Word count: 0 ");
ui->CharacterCount->setText(" Character count: 0 ");
ui->RemainingCharacters->setText(" Remaining characters: 65535 ");
ui->DocumentMode->setText(" Write Mode ");
}
// create status bar and populate it
QStatusBar *bar=new QStatusBar(this);
ui->GUI->addWidget(bar);
bar->addWidget(ui->WordCount, 0);
bar->addWidget(ui->CharacterCount,0);
bar->addWidget(ui->RemainingCharacters,0);
// empty slot to keep everything spaced properly
QLabel *spacer=new QLabel(" ");
bar->addWidget(spacer,1);
bar->addWidget(ui->DocumentMode,0);
bar->setSizeGripEnabled(false);
}
// Setup the form in EditMode
void Editor::InitiateEditMode(){
ui->PostEntry->setText("Update Entry");
ui->DocumentMode->setText(" Edit Mode ");
ui->DocumentMode->setAlignment(Qt::AlignRight);
// let the user change the date in edit mode -- NOT YET... that stil lcauses problems
//ui->EntryDate->setReadOnly(false);
}
// Load an entry and populate the form with it if EditMode is true
void Editor::LoadEntry(QString entry){
if(Buffer::backend=="MySQL"){
MySQLCore m;
QList<QString> list=m.RetrieveEntryFull(entry);
ui->EntryTitle->setText(list.at(0));
ui->EntryPost->setPlainText(list.at(5));
QDate postdate;
postdate.setDate(list.at(3).toInt(),list.at(1).toInt(),list.at(2).toInt());
ui->EntryDate->setDate(postdate);
// Do document statistics
DocumentStats();
}
}
//Update entry in the database
bool Editor::UpdateEntry(){
//Bugfix for 0.2
// clean commas out of the title so it doesn't mess up the array in MainWindow::CreateTree()
QString title=ui->EntryTitle->text();
title=title.replace(QString(","),QString(""));
QString body=ui->EntryPost->toPlainText();
QDate post_date=ui->EntryDate->date();
QString tags="null";
int day=post_date.day();
int month=post_date.month();
int year=post_date.year();
MySQLCore m;
bool success=m.Update(title,month,day,year,tags,body,Buffer::editentry);
return success;
}
// Prepare a new Entry and post it to database
bool Editor::NewEntry(){
using namespace std;
Editor::body=ui->EntryPost->toPlainText();
//Bugfix for 0.2
// clean commas out of the title so it doesn't mess up the array in MainWindow::CreateTree()
QString title=ui->EntryTitle->text();
title=title.replace(QString(","),QString(""));
Editor::title=title;
QDate post_date=ui->EntryDate->date();
Editor::day=post_date.day();
Editor::month=post_date.month();
Editor::year=post_date.year();
bool success=false;
cout << "OUTPUT: Adding new entry \"" + Editor::title.toStdString() + "\" to database " << Buffer::database_name.toStdString() << "...";
if(Buffer::backend=="MySQL"){
MySQLCore my;
success=my.AddEntry();
}
if(success){
cout << "SUCCESS!" << endl;
}
else{
cout << "FAILED" << endl;
}
return success;
}
Editor::~Editor()
{
delete ui;
}
//get current date
void Editor::SetDate(){
ui->EntryDate->setDate(QDate::currentDate());
}
// Automatically update window title with post title
void Editor::setTitle(QString title){
this->setWindowTitle(title + " -- RoboJournal");
if(title==NULL){
this->setWindowTitle("Untitled Entry -- RoboJournal");
}
}
// Set up Format button menu
void Editor::SetupMenu(){
QFont b("sans", 10, QFont::Bold);
b.setBold(true);
QFont i("sans", 10, QFont::StyleItalic);
i.setItalic(true);
QFont u("sans", 10, QFont::Normal);
u.setUnderline(true);
QMenu *formatmenu=new QMenu();
QAction *bold = new QAction("Bold", this);
QAction *italic = new QAction("Italic", this);
QAction *underline = new QAction("Underline", this);
QAction *h2 = new QAction("Subheader", this);
bold->setFont(b);
italic->setFont(i);
underline->setFont(u);
bold->setToolTip("Bold selected text");
italic->setToolTip("Italicize selected text");
underline->setToolTip("Underline selected text");
h2->setToolTip("Format selected text as sub-heading");
formatmenu->addAction(bold);
formatmenu->addAction(italic);
formatmenu->addAction(underline);
formatmenu->addSeparator();
formatmenu->addAction(h2);
ui->FormatButton->setMenu(formatmenu);
}
void Editor::on_EntryTitle_textChanged(const QString &title)
{
// pass title string to Editor::SetTitle() function... don't confuse this with QDialog::SetWindowTitle!!!
setTitle(title);
}
// keep track of document statistics
void Editor::DocumentStats(){
using namespace std;
QString data=ui->EntryPost->toPlainText();
int maxlength=65535;
int num=data.size();
int remaining=maxlength-num;
QString str_num;
QString str_rem;
str_num.append(QString("%1").arg(num));
str_rem.append(QString("%1").arg(remaining));
QString wordcount=WordCount(data);
ui->WordCount->setText(" Word count: " + wordcount + " ");
ui->CharacterCount->setText(" Character count: " + str_num + " ");
ui->RemainingCharacters->setText(" Remaining characters: " + str_rem + " ");
// prevent mysql data overflows, though it must have been one hell of a day if you can't say
// what's on your mind in less than 65k chars
if(remaining<0){
ui->RemainingCharacters->setText("<font color=\"red\"> Remaining characters: " +
str_rem +"</font> ");
ui->PostEntry->setDisabled(true);
}
else{
ui->PostEntry->setEnabled(true);
}
}
// Get word count from body. This needs to return a QString instead
// of Int because it needs to be displayed in a QLabel. Sure, we could
// return an int but that would require an extra conversion step.
QString Editor::WordCount(QString data){
// get rid of extra whitespace that would throw off the correct count
data=data.trimmed();
data=data.simplified();
// split the string into an array of words and count the size of array. That's our word count.
QStringList words=data.split(" ");
int count=words.count();
QString wordcount="0";
// solve a bug that always returns wordcount as 1 even if the body is blank
if(data.size()==0){
wordcount="0";
}
else{
wordcount=QString::number(count);
}
return wordcount;
}
void Editor::on_EntryPost_textChanged()
{
DocumentStats();
}
void Editor::on_UndoButton_clicked()
{
ui->EntryPost->undo();
}
void Editor::on_RedoButton_clicked()
{
ui->EntryPost->redo();
}
void Editor::on_Cancel_clicked()
{
if(Buffer::showwarnings){
ConfirmExit();
}
else{
this->done(0);
}
}
void Editor::on_CutButton_clicked()
{
ui->EntryPost->cut();
}
void Editor::on_CopyButton_clicked()
{
ui->EntryPost->copy();
}
void Editor::on_PasteButton_clicked()
{
ui->EntryPost->paste();
}
void Editor::on_PostEntry_clicked()
{
QMessageBox m;
if(Buffer::editmode){ // edit mode
if((ui->EntryPost->toPlainText()==NULL) || (ui->EntryTitle->text()==NULL)){
m.critical(this,"Error","The title and body of this entry must be filled in before RoboJournal can update it.");
}
else{
bool success=UpdateEntry();
if(success){
this->hide();
m.information(this,"RoboJoural", "<em>" + ui->EntryTitle->text() + "</em> was successfully updated.");
close();
}
}
}
else{ // new entry mode
if((ui->EntryPost->toPlainText()==NULL) || (ui->EntryTitle->text()==NULL)){
m.critical(this,"Error","The title and body of this entry must be filled in before RoboJournal can post it to the database.");
}
else{
bool success=NewEntry();
if(success){
this->hide();
m.information(this,"RoboJoural", "<em>" + ui->EntryTitle->text() + "</em> was successfully added to your journal.");
close();
}
}
}
}
|