<commit_before>/*******************************************************************
 KNotes -- Notes for the KDE project

 Copyright (c) 1997-2003, The KNotes Developers

 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 program 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 this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*******************************************************************/

#include <qlabel.h>
#include <qsizegrip.h>
#include <qbitmap.h>
#include <qcursor.h>
#include <qpointarray.h>
#include <qpainter.h>
#include <qpaintdevicemetrics.h>
#include <qsimplerichtext.h>

#include <kapplication.h>
#include <kdebug.h>
#include <kaction.h>
#include <kxmlguifactory.h>
#include <kprinter.h>
#include <klocale.h>
#include <kstandarddirs.h>
#include <ksimpleconfig.h>
#include <kiconloader.h>
#include <kmessagebox.h>
#include <kprocess.h>
#include <kinputdialog.h>
#include <kpopupmenu.h>
#include <kmdcodec.h>
#include <kglobalsettings.h>

#include "libkcal/journal.h"

#include "knote.h"
#include "knotebutton.h"
#include "knoteedit.h"
#include "knoteconfigdlg.h"
#include "version.h"

#include <kwin.h>
#include <netwm.h>

// fscking X headers
#ifdef FocusIn
#undef FocusIn
#endif
#ifdef FocusOut
#undef FocusOut
#endif

extern Atom qt_sm_client_id;

using namespace KCal;


KNote::KNote( KXMLGUIBuilder* builder, QDomDocument buildDoc, Journal *j,
              QWidget* parent, const char* name )
  : QFrame( parent, name, WStyle_Customize | WStyle_NoBorder | WDestructiveClose ),
    m_label( 0 ), m_button( 0 ), m_editor( 0 ), m_tool( 0 ), 
    m_journal( j )
{
    // to disable kwin's session management (ie. saving positions of windows) we need to
    // remove the session id from all note windows
    XChangeProperty( x11Display(), winId(), qt_sm_client_id, XA_STRING, 8,
        PropModeReplace, 0, 0 );

    // create the menu items for the note - not the editor...
    // rename, mail, print, insert date, close, delete, new note
    new KAction( i18n("New"), "filenew", 0, 
        this, SLOT(slotNewNote()), actionCollection(), "new_note" );
    new KAction( i18n("Rename..."), "text", 0, 
        this, SLOT(slotRename()), actionCollection(), "rename_note" );
    new KAction( i18n("Hide"), "fileclose" , 0, 
        this, SLOT(slotClose()), actionCollection(), "hide_note" );
    new KAction( i18n("Delete"), "knotes_delete", 0, 
        this, SLOT(slotKill()), actionCollection(), "delete_note" );

    new KAction( i18n("Insert Date"), "knotes_date", 0 , 
        this, SLOT(slotInsDate()), actionCollection(), "insert_date" );
    new KAction( i18n("Mail..."), "mail_send", 0, 
        this, SLOT(slotMail()), actionCollection(), "mail_note" );
    new KAction( i18n("Print..."), "fileprint", 0, 
        this, SLOT(slotPrint()), actionCollection(), "print_note" );
    new KAction( i18n("Preferences..."), "configure", 0, 
        this, SLOT(slotPreferences()), actionCollection(), "configure_note" );

    m_alwaysOnTop = new KToggleAction( i18n("Always on Top"), "attach", 0, 
        this, SLOT(slotToggleAlwaysOnTop()), actionCollection(), "always_on_top" );
    connect( m_alwaysOnTop, SIGNAL(toggled(bool)), m_alwaysOnTop, SLOT(setChecked(bool)) );

    m_toDesktop = new KListAction( i18n("To Desktop"), 0, 
        this, SLOT(slotPopupActionToDesktop(int)), actionCollection(), "to_desktop" );
    connect( m_toDesktop->popupMenu(), SIGNAL(aboutToShow()), this, SLOT(slotUpdateDesktopActions()) );

    // create the note header, button and label...
    m_button = new KNoteButton( "knotes_close", this );
    connect( m_button, SIGNAL(clicked()), this, SLOT(slotClose()) );

    m_label = new QLabel( this );
    m_label->installEventFilter( this );  // receive events (for dragging & action menu)
    setName( m_journal->summary() );      // don't worry, no signals are connected at this stage yet

    // create the toolbar
    m_tool = new QWidget( this, "toolbar" );
    m_tool->hide();

    // create the note editor
    m_editor = new KNoteEdit( m_tool, this );
    m_editor->installEventFilter( this ); // receive events (for modified)
    m_editor->viewport()->installEventFilter( this );

    setDOMDocument( buildDoc );
    factory = new KXMLGUIFactory( builder, this, "guifactory" );
    factory->addClient( this );

    m_menu = static_cast<KPopupMenu*>(factory->container( "note_context", this ));
    m_edit_menu = static_cast<KPopupMenu*>(factory->container( "note_edit", this ));

    setFocusProxy( m_editor );

    // create the resize handle
    m_editor->setCornerWidget( new QSizeGrip( this ) );
    uint width = m_editor->cornerWidget()->width();
    uint height = m_editor->cornerWidget()->height();
    QBitmap mask;
    mask.resize( width, height );
    mask.fill( color0 );
    QPointArray array;
    array.setPoints( 3, 0, height, width, height, width, 0 );
    QPainter p;
    p.begin( &mask );
    p.setBrush( color1 );
    p.drawPolygon( array );
    p.end();
    m_editor->cornerWidget()->setMask( mask );

    // set up the look&feel of the note
    setMinimumSize( 20, 20 );
    setFrameStyle( WinPanel | Raised );
    setLineWidth( 1 );

    m_editor->setMargin( 5 );
    m_editor->setFrameStyle( NoFrame );
    m_editor->setBackgroundMode( PaletteBase );

    // get the config attachment
    m_configFile = m_journal->attachments(CONFIG_MIME).first()->uri();

    // load the display configuration of the note
    KSimpleConfig config( m_configFile );
    config.setGroup( "Display" );
    width  = config.readUnsignedNumEntry( "width", 200 );
    height = config.readUnsignedNumEntry( "height", 200 );
    resize( width, height );

    config.setGroup( "WindowDisplay" );
    int note_desktop = config.readNumEntry( "desktop", KWin::currentDesktop() );
    ulong note_state = config.readUnsignedLongNumEntry( "state", NET::SkipTaskbar );
    QPoint default_position = QPoint( -1, -1 );
    QPoint position  = config.readPointEntry( "position", &default_position );

    KWin::setState( winId(), note_state );
    if ( note_state & NET::StaysOnTop )
        m_alwaysOnTop->setChecked( true );

    // let KWin do the placement if the position is illegal
    if ( position != default_position &&
            kapp->desktop()->rect().contains( position.x() + width, position.y() + height ) )<commit_msg>Grmbl, there *is* a bug in this code: one can move notes partly out of the screen, so now check if the note is at least partly visible.<commit_after>/*******************************************************************
 KNotes -- Notes for the KDE project

 Copyright (c) 1997-2003, The KNotes Developers

 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 program 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 this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*******************************************************************/

#include <qlabel.h>
#include <qsizegrip.h>
#include <qbitmap.h>
#include <qcursor.h>
#include <qpointarray.h>
#include <qpainter.h>
#include <qpaintdevicemetrics.h>
#include <qsimplerichtext.h>

#include <kapplication.h>
#include <kdebug.h>
#include <kaction.h>
#include <kxmlguifactory.h>
#include <kprinter.h>
#include <klocale.h>
#include <kstandarddirs.h>
#include <ksimpleconfig.h>
#include <kiconloader.h>
#include <kmessagebox.h>
#include <kprocess.h>
#include <kinputdialog.h>
#include <kpopupmenu.h>
#include <kmdcodec.h>
#include <kglobalsettings.h>

#include "libkcal/journal.h"

#include "knote.h"
#include "knotebutton.h"
#include "knoteedit.h"
#include "knoteconfigdlg.h"
#include "version.h"

#include <kwin.h>
#include <netwm.h>

// fscking X headers
#ifdef FocusIn
#undef FocusIn
#endif
#ifdef FocusOut
#undef FocusOut
#endif

extern Atom qt_sm_client_id;

using namespace KCal;


KNote::KNote( KXMLGUIBuilder* builder, QDomDocument buildDoc, Journal *j,
              QWidget* parent, const char* name )
  : QFrame( parent, name, WStyle_Customize | WStyle_NoBorder | WDestructiveClose ),
    m_label( 0 ), m_button( 0 ), m_editor( 0 ), m_tool( 0 ), 
    m_journal( j )
{
    // to disable kwin's session management (ie. saving positions of windows) we need to
    // remove the session id from all note windows
    XChangeProperty( x11Display(), winId(), qt_sm_client_id, XA_STRING, 8,
        PropModeReplace, 0, 0 );

    // create the menu items for the note - not the editor...
    // rename, mail, print, insert date, close, delete, new note
    new KAction( i18n("New"), "filenew", 0, 
        this, SLOT(slotNewNote()), actionCollection(), "new_note" );
    new KAction( i18n("Rename..."), "text", 0, 
        this, SLOT(slotRename()), actionCollection(), "rename_note" );
    new KAction( i18n("Hide"), "fileclose" , 0, 
        this, SLOT(slotClose()), actionCollection(), "hide_note" );
    new KAction( i18n("Delete"), "knotes_delete", 0, 
        this, SLOT(slotKill()), actionCollection(), "delete_note" );

    new KAction( i18n("Insert Date"), "knotes_date", 0 , 
        this, SLOT(slotInsDate()), actionCollection(), "insert_date" );
    new KAction( i18n("Mail..."), "mail_send", 0, 
        this, SLOT(slotMail()), actionCollection(), "mail_note" );
    new KAction( i18n("Print..."), "fileprint", 0, 
        this, SLOT(slotPrint()), actionCollection(), "print_note" );
    new KAction( i18n("Preferences..."), "configure", 0, 
        this, SLOT(slotPreferences()), actionCollection(), "configure_note" );

    m_alwaysOnTop = new KToggleAction( i18n("Always on Top"), "attach", 0, 
        this, SLOT(slotToggleAlwaysOnTop()), actionCollection(), "always_on_top" );
    connect( m_alwaysOnTop, SIGNAL(toggled(bool)), m_alwaysOnTop, SLOT(setChecked(bool)) );

    m_toDesktop = new KListAction( i18n("To Desktop"), 0, 
        this, SLOT(slotPopupActionToDesktop(int)), actionCollection(), "to_desktop" );
    connect( m_toDesktop->popupMenu(), SIGNAL(aboutToShow()), this, SLOT(slotUpdateDesktopActions()) );

    // create the note header, button and label...
    m_button = new KNoteButton( "knotes_close", this );
    connect( m_button, SIGNAL(clicked()), this, SLOT(slotClose()) );

    m_label = new QLabel( this );
    m_label->installEventFilter( this );  // receive events (for dragging & action menu)
    setName( m_journal->summary() );      // don't worry, no signals are connected at this stage yet

    // create the toolbar
    m_tool = new QWidget( this, "toolbar" );
    m_tool->hide();

    // create the note editor
    m_editor = new KNoteEdit( m_tool, this );
    m_editor->installEventFilter( this ); // receive events (for modified)
    m_editor->viewport()->installEventFilter( this );

    setDOMDocument( buildDoc );
    factory = new KXMLGUIFactory( builder, this, "guifactory" );
    factory->addClient( this );

    m_menu = static_cast<KPopupMenu*>(factory->container( "note_context", this ));
    m_edit_menu = static_cast<KPopupMenu*>(factory->container( "note_edit", this ));

    setFocusProxy( m_editor );

    // create the resize handle
    m_editor->setCornerWidget( new QSizeGrip( this ) );
    uint width = m_editor->cornerWidget()->width();
    uint height = m_editor->cornerWidget()->height();
    QBitmap mask;
    mask.resize( width, height );
    mask.fill( color0 );
    QPointArray array;
    array.setPoints( 3, 0, height, width, height, width, 0 );
    QPainter p;
    p.begin( &mask );
    p.setBrush( color1 );
    p.drawPolygon( array );
    p.end();
    m_editor->cornerWidget()->setMask( mask );

    // set up the look&feel of the note
    setMinimumSize( 20, 20 );
    setFrameStyle( WinPanel | Raised );
    setLineWidth( 1 );

    m_editor->setMargin( 5 );
    m_editor->setFrameStyle( NoFrame );
    m_editor->setBackgroundMode( PaletteBase );

    // get the config attachment
    m_configFile = m_journal->attachments(CONFIG_MIME).first()->uri();

    // load the display configuration of the note
    KSimpleConfig config( m_configFile );
    config.setGroup( "Display" );
    width  = config.readUnsignedNumEntry( "width", 200 );
    height = config.readUnsignedNumEntry( "height", 200 );
    resize( width, height );

    config.setGroup( "WindowDisplay" );
    int note_desktop = config.readNumEntry( "desktop", KWin::currentDesktop() );
    ulong note_state = config.readUnsignedLongNumEntry( "state", NET::SkipTaskbar );
    QPoint default_position = QPoint( -1000, -1000 );
    QPoint position  = config.readPointEntry( "position", &default_position );

    KWin::setState( winId(), note_state );
    if ( note_state & NET::StaysOnTop )
        m_alwaysOnTop->setChecked( true );

    // let KWin do the placement if the position is illegal
    if ( position != default_position &&
            kapp->desktop()->rect().intersects( QRect( position, size() ) ) )
{'fraction_non_alphanumeric': 0.1494035496072156, 'fraction_numerical': 0.012219959266802444, 'mean_word_length': 3.9581680490443563, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 107, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1950678', 'n_tokens_mistral': 4472, 'n_tokens_neox': 4214, 'n_words': 1258}