@@ -115,16 +115,50 @@ QString RichTextNote::settingsFilePath()
115115void RichTextNote::setFileName ( const QString & dirName )
116116{
117117 noteFileName = dirName;
118-
119118 // Если уже существует, то выходим
120119 if ( QDir ( noteFileName ).exists () )
120+ {
121+ // Настройка слежения за папкой заметки
122+ {
123+ // Прекращаем слежение за предыдущими файлами
124+ const QStringList & directories = fileSystemWatcher.directories ();
125+ if ( !directories.isEmpty () )
126+ fileSystemWatcher.removePaths ( directories );
127+
128+ const QStringList & files = fileSystemWatcher.files ();
129+ if ( !files.isEmpty () )
130+ fileSystemWatcher.removePaths ( files );
131+
132+ // Устанавливаем слежение за новыми
133+ fileSystemWatcher.addPath ( attachDirPath () ); // Папка с прикрепленными файлами
134+ fileSystemWatcher.addPath ( contentFilePath () ); // Файл, описывающий содержимое заметки (html)
135+ fileSystemWatcher.addPath ( settingsFilePath () ); // Файл, описывающий заметку - название, положение, размер, цвет и т.п.
136+
137+ // Так как папка у нас одна - папка с прикрепленными файлами, то обновляем только ее, а по хорошему нужно создать отдельный слот-обработчик.
138+ QObject::connect ( &fileSystemWatcher, SIGNAL (directoryChanged (QString)), SLOT (directoryChanged (QString)) );
139+ QObject::connect ( &fileSystemWatcher, SIGNAL (fileChanged (QString)), SLOT (fileChanged (QString)) );
140+ }
141+
121142 return ;
143+ }
122144
123145 // Создадим папку заметки с нужными папками и файлами
124146 QDir ().mkdir ( noteFileName );
125147 QDir ().mkdir ( attachDirPath () );
126148 QFile ( contentFilePath () ).open ( QIODevice::WriteOnly );
127149 QFile ( settingsFilePath () ).open ( QIODevice::WriteOnly );
150+
151+ // Настройка слежения за папкой заметки
152+ {
153+ // Устанавливаем слежение за новыми
154+ fileSystemWatcher.addPath ( attachDirPath () ); // Папка с прикрепленными файлами
155+ fileSystemWatcher.addPath ( contentFilePath () ); // Файл, описывающий содержимое заметки (html)
156+ fileSystemWatcher.addPath ( settingsFilePath () ); // Файл, описывающий заметку - название, положение, размер, цвет и т.п.
157+
158+ // Так как папка у нас одна - папка с прикрепленными файлами, то обновляем только ее, а по хорошему нужно создать отдельный слот-обработчик.
159+ QObject::connect ( &fileSystemWatcher, SIGNAL (directoryChanged (QString)), SLOT (directoryChanged (QString)) );
160+ QObject::connect ( &fileSystemWatcher, SIGNAL (fileChanged (QString)), SLOT (fileChanged (QString)) );
161+ }
128162}
129163
130164void RichTextNote::setDefaultSettingsFromMap (const QVariantMap & data )
@@ -341,20 +375,7 @@ void RichTextNote::setupGUI()
341375
342376void RichTextNote::save ()
343377{
344- mapSettings[ " Top" ] = isTop ();
345- mapSettings[ " ColorTitle" ] = titleColor ().name ();
346- mapSettings[ " ColorBody" ] = bodyColor ().name ();
347- mapSettings[ " Opacity" ] = opacity ();
348- mapSettings[ " Visible" ] = isVisible ();
349- mapSettings[ " Title" ] = title ();
350- mapSettings[ " FontTitle" ] = titleFont ().toString ();
351- mapSettings[ " Position" ] = pos ();
352- mapSettings[ " Size" ] = size ();
353-
354- QSettings ini ( settingsFilePath (), QSettings::IniFormat );
355- ini.setIniCodec ( " utf8" );
356- ini.setValue ( " Settings" , mapSettings );
357-
378+ saveSettings ();
358379 saveContent ();
359380 statusBar ()->showMessage ( tr ( " Save completed" ), 5000 );
360381 emit changed ( EventsNote::SaveEnded );
@@ -363,20 +384,9 @@ void RichTextNote::load()
363384{
364385 QSettings ini ( settingsFilePath (), QSettings::IniFormat );
365386 ini.setIniCodec ( " utf8" );
366- mapSettings = ini.value ( " Settings" ).toMap ();
367-
368- QString _title;
369- QFont _fontTitle;
370- QSize _size;
371- QPoint _position;
372- QColor _titleColor;
373- QColor _bodyColor;
374- bool _top;
375- qreal _opacity;
376- bool _visible;
377387
378388 // Если пуст, значит эта заметка новая -> берем параметры из дэфолтных настроек
379- if ( mapSettings .isEmpty () )
389+ if ( ini. value ( " Settings " ). toMap () .isEmpty () )
380390 {
381391 const QDateTime & currentDateTime = QDateTime::currentDateTime ();
382392 const QRect & desktop = QDesktopWidget ().geometry ();
@@ -386,16 +396,21 @@ void RichTextNote::load()
386396 mapSettings[ " Created" ] = currentDateTime;
387397 mapSettings[ " Modified" ] = currentDateTime;
388398
389- _title = TextTemplateParser::get ( defaultMapSettings[ " Title" ].toString () );
399+ QString _title = TextTemplateParser::get ( defaultMapSettings[ " Title" ].toString () );
400+ QFont _fontTitle;
401+ QSize _size = defaultMapSettings[ " Size" ].toSize ();
402+ QPoint _position;
403+
390404 _fontTitle.fromString ( defaultMapSettings[ " FontTitle" ].toString () );
391- _size = defaultMapSettings[ " Size" ].toSize ();
392405
393406 // TODO: доработать с учетом размера заметок, т.к. новые заметки может разместить за экран
394407 if ( randomPosition )
395408 _position = QPoint ( qrand () % desktop.width (), qrand () % desktop.height () );
396409 else
397410 _position = defaultMapSettings[ " Position" ].toPoint ();
398411
412+ QColor _titleColor;
413+ QColor _bodyColor;
399414 if ( randomColor )
400415 {
401416 _titleColor.setRgb ( qrand () % 0xff , qrand () % 0xff , qrand () % 0xff );
@@ -406,45 +421,79 @@ void RichTextNote::load()
406421 _bodyColor = QColor ( defaultMapSettings[ " ColorBody" ].toString () );
407422 }
408423
409- _top = defaultMapSettings[ " Top" ].toBool ();
410- _opacity = defaultMapSettings.value ( " Opacity" ).toDouble ();
411- _visible = defaultMapSettings.value ( " Visible" ).toBool ();
424+ bool _top = defaultMapSettings[ " Top" ].toBool ();
425+ qreal _opacity = defaultMapSettings.value ( " Opacity" ).toDouble ();
426+ bool _visible = defaultMapSettings.value ( " Visible" ).toBool ();
412427
413428 setText ( TextTemplateParser::get ( defaultMapSettings[ " Text" ].toString () ) );
414429
430+ setTitle ( _title );
431+ setTitleFont ( _fontTitle );
432+ resize ( _size );
433+ move ( _position );
434+ setTitleColor ( _titleColor );
435+ setBodyColor ( _bodyColor );
436+ setTop ( _top );
437+ setOpacity ( _opacity );
438+ setVisible ( _visible );
415439 } else
416440 {
417- _fontTitle.fromString ( mapSettings[ " FontTitle" ].toString () );
418- _title = mapSettings[ " Title" ].toString ();
419- _size = mapSettings[ " Size" ].toSize ();
420- _position = mapSettings[ " Position" ].toPoint ();
421- _titleColor = QColor ( mapSettings[ " ColorTitle" ].toString () );
422- _bodyColor = QColor ( mapSettings[ " ColorBody" ].toString () );
423- _top = mapSettings[ " Top" ].toBool ();
424- _opacity = mapSettings[ " Opacity" ].toDouble ();
425- _visible = mapSettings.value ( " Visible" ).toBool ();
426-
441+ loadSettings ();
427442 loadContent ();
428443 }
429444
430- setTitle ( _title );
431- setTitleFont ( _fontTitle );
432- resize ( _size );
433- move ( _position );
434- setTitleColor ( _titleColor );
435- setBodyColor ( _bodyColor );
436- setTop ( _top );
437- setOpacity ( _opacity );
438- setVisible ( _visible );
439-
440445 setActivateTimerAutosave ( defaultMapSettings[ " Autosave" ].toBool () );
441446 setIntervalAutosave ( defaultMapSettings[ " AutosaveInterval" ].toInt () );
442447
443448 updateStates ();
444449 emit changed ( EventsNote::LoadEnded );
445450}
451+ void RichTextNote::loadSettings ()
452+ {
453+ QSettings ini ( settingsFilePath (), QSettings::IniFormat );
454+ ini.setIniCodec ( " utf8" );
455+ mapSettings = ini.value ( " Settings" ).toMap ();
456+
457+ setTitle ( mapSettings[ " Title" ].toString () );
458+ setTitleFont ( mapSettings[ " FontTitle" ].toString () );
459+ resize ( mapSettings[ " Size" ].toSize () );
460+ move ( mapSettings[ " Position" ].toPoint () );
461+ setTitleColor ( QColor ( mapSettings[ " ColorTitle" ].toString () ) );
462+ setBodyColor ( QColor ( mapSettings[ " ColorBody" ].toString () ) );
463+ setTop ( mapSettings[ " Top" ].toBool () );
464+ setOpacity ( mapSettings[ " Opacity" ].toDouble () );
465+ setVisible ( mapSettings.value ( " Visible" ).toBool () );
466+
467+ updateStates ();
468+ emit changed ( EventsNote::LoadEnded );
469+ }
470+ void RichTextNote::saveSettings ()
471+ {
472+ // Не нужно реагировать на изменение при сохранении
473+ fileSystemWatcher.blockSignals ( true );
474+
475+ mapSettings[ " Top" ] = isTop ();
476+ mapSettings[ " ColorTitle" ] = titleColor ().name ();
477+ mapSettings[ " ColorBody" ] = bodyColor ().name ();
478+ mapSettings[ " Opacity" ] = opacity ();
479+ mapSettings[ " Visible" ] = isVisible ();
480+ mapSettings[ " Title" ] = title ();
481+ mapSettings[ " FontTitle" ] = titleFont ().toString ();
482+ mapSettings[ " Position" ] = pos ();
483+ mapSettings[ " Size" ] = size ();
484+
485+ QSettings ini ( settingsFilePath (), QSettings::IniFormat );
486+ ini.setIniCodec ( " utf8" );
487+ ini.setValue ( " Settings" , mapSettings );
488+ ini.sync ();
489+
490+ fileSystemWatcher.blockSignals ( false );
491+ }
446492void RichTextNote::saveContent ()
447493{
494+ // Не нужно реагировать на изменение при сохранении
495+ fileSystemWatcher.blockSignals ( true );
496+
448497 QFile content ( contentFilePath () );
449498 if ( !content.open ( QIODevice::Truncate | QIODevice::WriteOnly ) )
450499 {
@@ -457,11 +506,22 @@ void RichTextNote::saveContent()
457506 in << text ();
458507 content.close ();
459508
509+ fileSystemWatcher.blockSignals ( false );
510+
460511 updateStates ();
461512}
462513void RichTextNote::loadContent ()
463514{
464- editor.setSource ( QUrl::fromLocalFile ( contentFilePath () ) );
515+ // editor.setSource( QUrl::fromLocalFile( contentFilePath() ) );
516+
517+ QFile file ( contentFilePath () );
518+ if ( !file.open ( QIODevice::ReadOnly ) )
519+ return ;
520+
521+ QTextStream in ( &file );
522+ in.setCodec ( QTextCodec::codecForName ( codec ) );
523+
524+ editor.setHtml ( in.readAll () );
465525}
466526void RichTextNote::setText ( const QString & str )
467527{
@@ -476,8 +536,17 @@ QString RichTextNote::text()
476536}
477537void RichTextNote::removeDir ()
478538{
539+ // Прекращаем слежение за файлами и папками
540+ const QStringList & directories = fileSystemWatcher.directories ();
541+ if ( !directories.isEmpty () )
542+ fileSystemWatcher.removePaths ( directories );
543+
544+ const QStringList & files = fileSystemWatcher.files ();
545+ if ( !files.isEmpty () )
546+ fileSystemWatcher.removePaths ( files );
547+
479548 if ( !removePath ( fileName () ) )
480- QMessageBox::warning ( this , tr ( " Warning" ), tr ( " I can not delete" ) );
549+ QMessageBox::warning ( this , tr ( " Warning" ), tr ( " I can not delete" ) );
481550}
482551void RichTextNote::remove ()
483552{
@@ -705,10 +774,15 @@ void RichTextNote::insertImage( const QPixmap & pixmap )
705774
706775QString RichTextNote::attach ( const QString & fileName )
707776{
777+ fileSystemWatcher.blockSignals ( true );
778+
708779 QString newFileName = attachDirPath () + QDir::separator () + QFileInfo ( fileName ).fileName ();
709780 QFile::copy ( fileName, newFileName );
710781
711782 attachModel.appendRow ( new QStandardItem ( QFileInfo ( fileName ).fileName () ) );
783+
784+ fileSystemWatcher.blockSignals ( false );
785+
712786 emit changed ( EventsNote::ChangeAttach );
713787 return newFileName;
714788}
@@ -807,6 +881,21 @@ void RichTextNote::doubleClickingOnTitle()
807881 }
808882}
809883
884+ void RichTextNote::directoryChanged ( const QString & name )
885+ {
886+ // Если это была папка с прикрепленными файлами
887+ if ( name == attachDirPath () )
888+ updateAttachList ();
889+ }
890+ void RichTextNote::fileChanged ( const QString & name )
891+ {
892+ if ( name == contentFilePath () )
893+ loadContent ();
894+
895+ else if ( name == settingsFilePath () )
896+ loadSettings ();
897+ }
898+
810899void RichTextNote::enterEvent ( QEvent * )
811900{
812901 QPropertyAnimation * animation = new QPropertyAnimation ( this , " windowOpacity" );
0 commit comments