summaryrefslogtreecommitdiffstats
path: root/src/gui/newfiledlg.cpp
blob: 09c0784e5c4a4fe545e5951f68e5f1c1080fe0e2 (plain)
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
/***************************************************************************
 *   Copyright (C) 2003-2005 by David Saxton                               *
 *   david@bluehaze.org                                                    *
 *                                                                         *
 *   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.                                   *
 ***************************************************************************/

#include "config.h"
#include "document.h"
#include "microinfo.h"
#include "newfiledlg.h"
#include "newfilewidget.h"
#include "microlibrary.h"
#include "microselectwidget.h"
#include "projectmanager.h"
#include "textdocument.h"

#include <kcombobox.h>
#include <kdebug.h>
#include <klineedit.h>
#include <kiconview.h>
#include <tdelocale.h>
#include <kiconloader.h>

#include <tqcanvas.h>
#include <tqcheckbox.h>
#include <tqdir.h>
#include <tqfile.h>
#include <tqlabel.h>
#include <tqpaintdevicemetrics.h>


NewFileDlg::NewFileDlg( TQWidget *parent )
    : KDialogBase( parent, "newfiledlg", true, "New File", KDialogBase::Ok|KDialogBase::Cancel, KDialogBase::Ok, true )
{
	m_pMainParent = parent;
    m_bAccepted = false;
    m_pNewFileWidget = new NewFileWidget(this);

	 m_pNewFileWidget->typeIconView->setSelectionMode(TQIconView::Single);
	 m_pNewFileWidget->typeIconView->setMode(TDEIconView::Select);
    
	TDEIconLoader *loader = TDEGlobal::iconLoader();
	
	TQValueList<TQIconViewItem*> items;
	
	items << new TQIconViewItem(m_pNewFileWidget->typeIconView,"Assembly Code (.asm)", loader->loadIcon( ""text-x-src", TDEIcon::NoGroup, TDEIcon::SizeHuge ) );
	items << new TQIconViewItem(m_pNewFileWidget->typeIconView,"C (.c)", loader->loadIcon( ""text-x-csrc", TDEIcon::NoGroup, TDEIcon::SizeHuge ) );
	items << new TQIconViewItem(m_pNewFileWidget->typeIconView,"Circuit (.circuit)", loader->loadIcon( "ktechlab_circuit", TDEIcon::NoGroup, TDEIcon::SizeHuge ) );
	items << new TQIconViewItem(m_pNewFileWidget->typeIconView,"FlowCode (.flowcode)", loader->loadIcon( "ktechlab_flowcode", TDEIcon::NoGroup, TDEIcon::SizeHuge ) );
#ifdef MECHANICS
	items << new TQIconViewItem(m_pNewFileWidget->typeIconView,"Mechanics (.mechanics)", loader->loadIcon( "application-x-executable", TDEIcon::NoGroup, TDEIcon::SizeHuge ) );
#endif
	items << new TQIconViewItem(m_pNewFileWidget->typeIconView,"Microbe (.microbe)", loader->loadIcon( "ktechlab_microbe", TDEIcon::NoGroup, TDEIcon::SizeHuge ) );
	
	unsigned minWidth = 20 + m_pNewFileWidget->typeIconView->spacing() * items.size();
	int minHeight = 0;
	
	const TQValueList<TQIconViewItem*>::iterator end = items.end();
	for ( TQValueList<TQIconViewItem*>::iterator it = items.begin(); it != end; ++it )
	{
		(*it)->setDragEnabled(false);
		minWidth += (*it)->width();
		minHeight = TQMAX( minHeight, (*it)->height()+20 );
	}
	
	m_pNewFileWidget->typeIconView->setMinimumSize( minWidth, minHeight );
	m_pNewFileWidget->typeIconView->setCurrentItem(items[3]);
	m_pNewFileWidget->addToProjectCheck->setChecked( ProjectManager::self()->currentProject() );
	m_pNewFileWidget->addToProjectCheck->setEnabled( ProjectManager::self()->currentProject() );
	microSelectWidget()->setAllowedFlowCodeSupport( MicroInfo::FullSupport | MicroInfo::PartialSupport );
    
	setMainWidget(m_pNewFileWidget);
    
	// Our behaviour is to have single click selects and double click accepts the dialog
	connect( m_pNewFileWidget->typeIconView, TQT_SIGNAL(selectionChanged(TQIconViewItem*)), this, TQT_SLOT(fileTypeChanged(TQIconViewItem*)) );
	connect( m_pNewFileWidget->typeIconView, TQT_SIGNAL(doubleClicked(TQIconViewItem*)), this, TQT_SLOT(accept()));

    setAcceptDrops(true);

	m_pNewFileWidget->typeIconView->adjustSize();
	m_pNewFileWidget->adjustSize();
	adjustSize();
}

void NewFileDlg::accept()
{
	hide();
	m_bAccepted = true;
	
	const TQString fileText = m_pNewFileWidget->typeIconView->currentItem()->text();
	
	if		( fileText.contains(".flowcode") )
		m_fileType = Document::dt_flowcode;
	
	else if ( fileText.contains(".circuit") )
		m_fileType = Document::dt_circuit;
	
	else if ( fileText.contains(".mechanics") )
		m_fileType = Document::dt_mechanics;
	
	else if ( fileText.contains(".asm") )
	{
		m_fileType = Document::dt_text;
		m_codeType = TextDocument::ct_asm;
	}
	
	else if ( fileText.contains(".basic") || fileText.contains(".microbe") )
	{
		m_fileType = Document::dt_text;
		m_codeType = TextDocument::ct_microbe;
	}
	
	else if (fileText.contains(".c") )
	{
		m_fileType = Document::dt_text;
		m_codeType = TextDocument::ct_c;
	}
	
	else
		m_fileType = Document::dt_text;

	m_bAddToProject = m_pNewFileWidget->addToProjectCheck->isChecked();
	
	m_microID = m_pNewFileWidget->m_pMicroSelect->micro();
}


void NewFileDlg::reject()
{
    m_bAccepted = false;
}


void NewFileDlg::fileTypeChanged( TQIconViewItem *item )
{
	m_pNewFileWidget->m_pMicroSelect->setEnabled(
			item->text().contains(".flowcode") );
}


MicroSelectWidget * NewFileDlg::microSelectWidget() const
{
	return m_pNewFileWidget->m_pMicroSelect;
}


#include "newfiledlg.moc"